1 // SPDX-License-Identifier: GPL-2.0+
3 * KP2000 SPI controller driver
5 * Copyright (C) 2014-2018 Daktronics
6 * Author: Matt Sickler <matt.sickler@daktronics.com>
7 * Very loosely based on spi-omap2-mcspi.c
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/interrupt.h>
13 #include <linux/io-64-nonatomic-lo-hi.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
21 #include <linux/slab.h>
22 #include <linux/pm_runtime.h>
24 #include <linux/of_device.h>
25 #include <linux/gcd.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/flash.h>
28 #include <linux/mtd/partitions.h>
32 static struct mtd_partition p2kr0_spi0_parts[] = {
33 { .name = "SLOT_0", .size = 7798784, .offset = 0, },
34 { .name = "SLOT_1", .size = 7798784, .offset = MTDPART_OFS_NXTBLK},
35 { .name = "SLOT_2", .size = 7798784, .offset = MTDPART_OFS_NXTBLK},
36 { .name = "SLOT_3", .size = 7798784, .offset = MTDPART_OFS_NXTBLK},
37 { .name = "CS0_EXTRA", .size = MTDPART_SIZ_FULL, .offset = MTDPART_OFS_NXTBLK},
40 static struct mtd_partition p2kr0_spi1_parts[] = {
41 { .name = "SLOT_4", .size = 7798784, .offset = 0, },
42 { .name = "SLOT_5", .size = 7798784, .offset = MTDPART_OFS_NXTBLK},
43 { .name = "SLOT_6", .size = 7798784, .offset = MTDPART_OFS_NXTBLK},
44 { .name = "SLOT_7", .size = 7798784, .offset = MTDPART_OFS_NXTBLK},
45 { .name = "CS1_EXTRA", .size = MTDPART_SIZ_FULL, .offset = MTDPART_OFS_NXTBLK},
48 static struct flash_platform_data p2kr0_spi0_pdata = {
50 .nr_parts = ARRAY_SIZE(p2kr0_spi0_parts),
51 .parts = p2kr0_spi0_parts,
53 static struct flash_platform_data p2kr0_spi1_pdata = {
55 .nr_parts = ARRAY_SIZE(p2kr0_spi1_parts),
56 .parts = p2kr0_spi1_parts,
59 static struct spi_board_info p2kr0_board_info[] = {
61 .modalias = "n25q256a11",
65 .platform_data = &p2kr0_spi0_pdata
68 .modalias = "n25q256a11",
72 .platform_data = &p2kr0_spi1_pdata
79 #define KP_SPI_REG_CONFIG 0x0 /* 0x00 */
80 #define KP_SPI_REG_STATUS 0x1 /* 0x08 */
81 #define KP_SPI_REG_FFCTRL 0x2 /* 0x10 */
82 #define KP_SPI_REG_TXDATA 0x3 /* 0x18 */
83 #define KP_SPI_REG_RXDATA 0x4 /* 0x20 */
85 #define KP_SPI_CLK 48000000
86 #define KP_SPI_MAX_FIFODEPTH 64
87 #define KP_SPI_MAX_FIFOWCNT 0xFFFF
89 #define KP_SPI_REG_CONFIG_TRM_TXRX 0
90 #define KP_SPI_REG_CONFIG_TRM_RX 1
91 #define KP_SPI_REG_CONFIG_TRM_TX 2
93 #define KP_SPI_REG_STATUS_RXS 0x01
94 #define KP_SPI_REG_STATUS_TXS 0x02
95 #define KP_SPI_REG_STATUS_EOT 0x04
96 #define KP_SPI_REG_STATUS_TXFFE 0x10
97 #define KP_SPI_REG_STATUS_TXFFF 0x20
98 #define KP_SPI_REG_STATUS_RXFFE 0x40
99 #define KP_SPI_REG_STATUS_RXFFF 0x80
105 struct spi_master *master;
110 struct kp_spi_controller_state {
115 union kp_spi_config {
116 /* use this to access individual elements */
117 struct __packed spi_config_bitfield {
118 unsigned int pha : 1; /* spim_clk Phase */
119 unsigned int pol : 1; /* spim_clk Polarity */
120 unsigned int epol : 1; /* spim_csx Polarity */
121 unsigned int dpe : 1; /* Transmission Enable */
122 unsigned int wl : 5; /* Word Length */
124 unsigned int trm : 2; /* TxRx Mode */
125 unsigned int cs : 4; /* Chip Select */
126 unsigned int wcnt : 7; /* Word Count */
127 unsigned int ffen : 1; /* FIFO Enable */
128 unsigned int spi_en : 1; /* SPI Enable */
131 /* use this to grab the whole register */
135 union kp_spi_status {
136 struct __packed spi_status_bitfield {
137 unsigned int rx : 1; /* Rx Status */
138 unsigned int tx : 1; /* Tx Status */
139 unsigned int eo : 1; /* End of Transfer */
141 unsigned int txffe : 1; /* Tx FIFO Empty */
142 unsigned int txfff : 1; /* Tx FIFO Full */
143 unsigned int rxffe : 1; /* Rx FIFO Empty */
144 unsigned int rxfff : 1; /* Rx FIFO Full */
150 union kp_spi_ffctrl {
151 struct __packed spi_ffctrl_bitfield {
152 unsigned int ffstart : 1; /* FIFO Start */
162 kp_spi_read_reg(struct kp_spi_controller_state *cs, int idx)
164 u64 __iomem *addr = cs->base;
168 if ((idx == KP_SPI_REG_CONFIG) && (cs->conf_cache >= 0))
169 return cs->conf_cache;
176 kp_spi_write_reg(struct kp_spi_controller_state *cs, int idx, u64 val)
178 u64 __iomem *addr = cs->base;
182 if (idx == KP_SPI_REG_CONFIG)
183 cs->conf_cache = val;
187 kp_spi_wait_for_reg_bit(struct kp_spi_controller_state *cs, int idx,
190 unsigned long timeout;
192 timeout = jiffies + msecs_to_jiffies(1000);
193 while (!(kp_spi_read_reg(cs, idx) & bit)) {
194 if (time_after(jiffies, timeout)) {
195 if (!(kp_spi_read_reg(cs, idx) & bit))
206 kp_spi_txrx_pio(struct spi_device *spidev, struct spi_transfer *transfer)
208 struct kp_spi_controller_state *cs = spidev->controller_state;
209 unsigned int count = transfer->len;
210 unsigned int c = count;
214 u8 *rx = transfer->rx_buf;
215 const u8 *tx = transfer->tx_buf;
219 for (i = 0 ; i < c ; i++) {
222 res = kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
223 KP_SPI_REG_STATUS_TXS);
227 kp_spi_write_reg(cs, KP_SPI_REG_TXDATA, val);
232 for (i = 0 ; i < c ; i++) {
235 kp_spi_write_reg(cs, KP_SPI_REG_TXDATA, 0x00);
236 res = kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
237 KP_SPI_REG_STATUS_RXS);
241 test = kp_spi_read_reg(cs, KP_SPI_REG_RXDATA);
247 if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
248 KP_SPI_REG_STATUS_EOT) < 0) {
249 //TODO: Figure out how to abort transaction??
250 //Ths has never happened in practice though...
261 kp_spi_setup(struct spi_device *spidev)
263 union kp_spi_config sc;
264 struct kp_spi *kpspi = spi_master_get_devdata(spidev->master);
265 struct kp_spi_controller_state *cs;
267 /* setup controller state */
268 cs = spidev->controller_state;
270 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
273 cs->base = kpspi->base;
275 spidev->controller_state = cs;
278 /* set config register */
279 sc.bitfield.wl = spidev->bits_per_word - 1;
280 sc.bitfield.cs = spidev->chip_select;
281 sc.bitfield.spi_en = 0;
283 sc.bitfield.ffen = 0;
284 kp_spi_write_reg(spidev->controller_state, KP_SPI_REG_CONFIG, sc.reg);
289 kp_spi_transfer_one_message(struct spi_master *master, struct spi_message *m)
291 struct kp_spi_controller_state *cs;
292 struct spi_device *spidev;
293 struct kp_spi *kpspi;
294 struct spi_transfer *transfer;
295 union kp_spi_config sc;
299 kpspi = spi_master_get_devdata(master);
300 m->actual_length = 0;
303 cs = spidev->controller_state;
305 /* reject invalid messages and transfers */
306 if (list_empty(&m->transfers))
310 list_for_each_entry(transfer, &m->transfers, transfer_list) {
311 const void *tx_buf = transfer->tx_buf;
312 void *rx_buf = transfer->rx_buf;
313 unsigned int len = transfer->len;
315 if (transfer->speed_hz > KP_SPI_CLK ||
316 (len && !(rx_buf || tx_buf))) {
317 dev_dbg(kpspi->dev, " transfer: %d Hz, %d %s%s, %d bpw\n",
322 transfer->bits_per_word);
323 dev_dbg(kpspi->dev, " transfer -EINVAL\n");
326 if (transfer->speed_hz &&
327 transfer->speed_hz < (KP_SPI_CLK >> 15)) {
328 dev_dbg(kpspi->dev, "speed_hz %d below minimum %d Hz\n",
331 dev_dbg(kpspi->dev, " speed_hz -EINVAL\n");
336 /* assert chip select to start the sequence*/
337 sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
338 sc.bitfield.spi_en = 1;
339 kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
342 if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
343 KP_SPI_REG_STATUS_EOT) < 0) {
344 dev_info(kpspi->dev, "EOT timed out\n");
348 /* do the transfers for this message */
349 list_for_each_entry(transfer, &m->transfers, transfer_list) {
350 if (!transfer->tx_buf && !transfer->rx_buf &&
358 unsigned int word_len = spidev->bits_per_word;
361 /* set up the transfer... */
362 sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
365 if (transfer->tx_buf)
366 sc.bitfield.trm = KP_SPI_REG_CONFIG_TRM_TX;
367 else if (transfer->rx_buf)
368 sc.bitfield.trm = KP_SPI_REG_CONFIG_TRM_RX;
371 if (transfer->bits_per_word)
372 word_len = transfer->bits_per_word;
373 sc.bitfield.wl = word_len - 1;
376 sc.bitfield.cs = spidev->chip_select;
378 /* ...and write the new settings */
379 kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
381 /* do the transfer */
382 count = kp_spi_txrx_pio(spidev, transfer);
383 m->actual_length += count;
385 if (count != transfer->len) {
391 if (transfer->delay_usecs)
392 udelay(transfer->delay_usecs);
395 /* de-assert chip select to end the sequence */
396 sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
397 sc.bitfield.spi_en = 0;
398 kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
402 spi_finalize_current_message(master);
411 kp_spi_cleanup(struct spi_device *spidev)
413 struct kp_spi_controller_state *cs = spidev->controller_state;
422 kp_spi_probe(struct platform_device *pldev)
424 struct kpc_core_device_platdata *drvdata;
425 struct spi_master *master;
426 struct kp_spi *kpspi;
431 drvdata = pldev->dev.platform_data;
433 dev_err(&pldev->dev, "%s: platform_data is NULL\n", __func__);
437 master = spi_alloc_master(&pldev->dev, sizeof(struct kp_spi));
439 dev_err(&pldev->dev, "%s: master allocation failed\n",
444 /* set up the spi functions */
445 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
446 master->bits_per_word_mask = (unsigned int)SPI_BPW_RANGE_MASK(4, 32);
447 master->setup = kp_spi_setup;
448 master->transfer_one_message = kp_spi_transfer_one_message;
449 master->cleanup = kp_spi_cleanup;
451 platform_set_drvdata(pldev, master);
453 kpspi = spi_master_get_devdata(master);
454 kpspi->master = master;
455 kpspi->dev = &pldev->dev;
457 master->num_chipselect = 4;
459 master->bus_num = pldev->id;
461 r = platform_get_resource(pldev, IORESOURCE_MEM, 0);
463 dev_err(&pldev->dev, "%s: Unable to get platform resources\n",
469 kpspi->base = devm_ioremap_nocache(&pldev->dev, r->start,
472 status = spi_register_master(master);
474 dev_err(&pldev->dev, "Unable to register SPI device\n");
478 /* register the slave boards */
479 #define NEW_SPI_DEVICE_FROM_BOARD_INFO_TABLE(table) \
480 for (i = 0 ; i < ARRAY_SIZE(table) ; i++) { \
481 spi_new_device(master, &(table[i])); \
484 switch ((drvdata->card_id & 0xFFFF0000) >> 16) {
485 case PCI_DEVICE_ID_DAKTRONICS_KADOKA_P2KR0:
486 NEW_SPI_DEVICE_FROM_BOARD_INFO_TABLE(p2kr0_board_info);
489 dev_err(&pldev->dev, "Unknown hardware, cant know what partition table to use!\n");
496 spi_master_put(master);
501 kp_spi_remove(struct platform_device *pldev)
503 struct spi_master *master = platform_get_drvdata(pldev);
505 spi_unregister_master(master);
509 static struct platform_driver kp_spi_driver = {
511 .name = KP_DRIVER_NAME_SPI,
513 .probe = kp_spi_probe,
514 .remove = kp_spi_remove,
517 module_platform_driver(kp_spi_driver);
518 MODULE_LICENSE("GPL");
519 MODULE_ALIAS("platform:kp_spi");