1 // SPDX-License-Identifier: GPL-2.0+
3 * KP2000 SPI controller driver
5 * Copyright (C) 2014-2018 Daktronics
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,
54 static struct flash_platform_data p2kr0_spi1_pdata = {
56 .nr_parts = ARRAY_SIZE(p2kr0_spi1_parts),
57 .parts = p2kr0_spi1_parts,
60 static struct spi_board_info p2kr0_board_info[] = {
62 .modalias = "n25q256a11",
66 .platform_data = &p2kr0_spi0_pdata
69 .modalias = "n25q256a11",
73 .platform_data = &p2kr0_spi1_pdata
80 #define KP_SPI_REG_CONFIG 0x0 /* 0x00 */
81 #define KP_SPI_REG_STATUS 0x1 /* 0x08 */
82 #define KP_SPI_REG_FFCTRL 0x2 /* 0x10 */
83 #define KP_SPI_REG_TXDATA 0x3 /* 0x18 */
84 #define KP_SPI_REG_RXDATA 0x4 /* 0x20 */
86 #define KP_SPI_CLK 48000000
87 #define KP_SPI_MAX_FIFODEPTH 64
88 #define KP_SPI_MAX_FIFOWCNT 0xFFFF
90 #define KP_SPI_REG_CONFIG_TRM_TXRX 0
91 #define KP_SPI_REG_CONFIG_TRM_RX 1
92 #define KP_SPI_REG_CONFIG_TRM_TX 2
94 #define KP_SPI_REG_STATUS_RXS 0x01
95 #define KP_SPI_REG_STATUS_TXS 0x02
96 #define KP_SPI_REG_STATUS_EOT 0x04
97 #define KP_SPI_REG_STATUS_TXFFE 0x10
98 #define KP_SPI_REG_STATUS_TXFFF 0x20
99 #define KP_SPI_REG_STATUS_RXFFE 0x40
100 #define KP_SPI_REG_STATUS_RXFFF 0x80
106 struct spi_master *master;
111 struct kp_spi_controller_state {
116 union kp_spi_config {
117 /* use this to access individual elements */
118 struct __packed spi_config_bitfield {
119 unsigned int pha : 1; /* spim_clk Phase */
120 unsigned int pol : 1; /* spim_clk Polarity */
121 unsigned int epol : 1; /* spim_csx Polarity */
122 unsigned int dpe : 1; /* Transmission Enable */
123 unsigned int wl : 5; /* Word Length */
125 unsigned int trm : 2; /* TxRx Mode */
126 unsigned int cs : 4; /* Chip Select */
127 unsigned int wcnt : 7; /* Word Count */
128 unsigned int ffen : 1; /* FIFO Enable */
129 unsigned int spi_en : 1; /* SPI Enable */
132 /* use this to grab the whole register */
136 union kp_spi_status {
137 struct __packed spi_status_bitfield {
138 unsigned int rx : 1; /* Rx Status */
139 unsigned int tx : 1; /* Tx Status */
140 unsigned int eo : 1; /* End of Transfer */
142 unsigned int txffe : 1; /* Tx FIFO Empty */
143 unsigned int txfff : 1; /* Tx FIFO Full */
144 unsigned int rxffe : 1; /* Rx FIFO Empty */
145 unsigned int rxfff : 1; /* Rx FIFO Full */
151 union kp_spi_ffctrl {
152 struct __packed spi_ffctrl_bitfield {
153 unsigned int ffstart : 1; /* FIFO Start */
163 kp_spi_read_reg(struct kp_spi_controller_state *cs, int idx)
165 u64 __iomem *addr = cs->base;
168 if ((idx == KP_SPI_REG_CONFIG) && (cs->conf_cache >= 0))
169 return cs->conf_cache;
175 kp_spi_write_reg(struct kp_spi_controller_state *cs, int idx, u64 val)
177 u64 __iomem *addr = cs->base;
181 if (idx == KP_SPI_REG_CONFIG)
182 cs->conf_cache = val;
186 kp_spi_wait_for_reg_bit(struct kp_spi_controller_state *cs, int idx,
189 unsigned long timeout;
191 timeout = jiffies + msecs_to_jiffies(1000);
192 while (!(kp_spi_read_reg(cs, idx) & bit)) {
193 if (time_after(jiffies, timeout)) {
194 if (!(kp_spi_read_reg(cs, idx) & bit))
205 kp_spi_txrx_pio(struct spi_device *spidev, struct spi_transfer *transfer)
207 struct kp_spi_controller_state *cs = spidev->controller_state;
208 unsigned int count = transfer->len;
209 unsigned int c = count;
213 u8 *rx = transfer->rx_buf;
214 const u8 *tx = transfer->tx_buf;
218 for (i = 0 ; i < c ; i++) {
221 res = kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
222 KP_SPI_REG_STATUS_TXS);
226 kp_spi_write_reg(cs, KP_SPI_REG_TXDATA, val);
230 for (i = 0 ; i < c ; i++) {
233 kp_spi_write_reg(cs, KP_SPI_REG_TXDATA, 0x00);
234 res = kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
235 KP_SPI_REG_STATUS_RXS);
239 test = kp_spi_read_reg(cs, KP_SPI_REG_RXDATA);
245 if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
246 KP_SPI_REG_STATUS_EOT) < 0) {
247 //TODO: Figure out how to abort transaction??
248 //Ths has never happened in practice though...
259 kp_spi_setup(struct spi_device *spidev)
261 union kp_spi_config sc;
262 struct kp_spi *kpspi = spi_master_get_devdata(spidev->master);
263 struct kp_spi_controller_state *cs;
265 /* setup controller state */
266 cs = spidev->controller_state;
268 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
271 cs->base = kpspi->base;
273 spidev->controller_state = cs;
276 /* set config register */
277 sc.bitfield.wl = spidev->bits_per_word - 1;
278 sc.bitfield.cs = spidev->chip_select;
279 sc.bitfield.spi_en = 0;
281 sc.bitfield.ffen = 0;
282 kp_spi_write_reg(spidev->controller_state, KP_SPI_REG_CONFIG, sc.reg);
287 kp_spi_transfer_one_message(struct spi_master *master, struct spi_message *m)
289 struct kp_spi_controller_state *cs;
290 struct spi_device *spidev;
291 struct kp_spi *kpspi;
292 struct spi_transfer *transfer;
293 union kp_spi_config sc;
297 kpspi = spi_master_get_devdata(master);
298 m->actual_length = 0;
301 cs = spidev->controller_state;
303 /* reject invalid messages and transfers */
304 if (list_empty(&m->transfers))
308 list_for_each_entry(transfer, &m->transfers, transfer_list) {
309 const void *tx_buf = transfer->tx_buf;
310 void *rx_buf = transfer->rx_buf;
311 unsigned int len = transfer->len;
313 if (transfer->speed_hz > KP_SPI_CLK ||
314 (len && !(rx_buf || tx_buf))) {
315 dev_dbg(kpspi->dev, " transfer: %d Hz, %d %s%s, %d bpw\n",
320 transfer->bits_per_word);
321 dev_dbg(kpspi->dev, " transfer -EINVAL\n");
324 if (transfer->speed_hz &&
325 transfer->speed_hz < (KP_SPI_CLK >> 15)) {
326 dev_dbg(kpspi->dev, "speed_hz %d below minimum %d Hz\n",
329 dev_dbg(kpspi->dev, " speed_hz -EINVAL\n");
334 /* assert chip select to start the sequence*/
335 sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
336 sc.bitfield.spi_en = 1;
337 kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
340 if (kp_spi_wait_for_reg_bit(cs, KP_SPI_REG_STATUS,
341 KP_SPI_REG_STATUS_EOT) < 0) {
342 dev_info(kpspi->dev, "EOT timed out\n");
346 /* do the transfers for this message */
347 list_for_each_entry(transfer, &m->transfers, transfer_list) {
348 if (!transfer->tx_buf && !transfer->rx_buf &&
356 unsigned int word_len = spidev->bits_per_word;
359 /* set up the transfer... */
360 sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
363 if (transfer->tx_buf)
364 sc.bitfield.trm = KP_SPI_REG_CONFIG_TRM_TX;
365 else if (transfer->rx_buf)
366 sc.bitfield.trm = KP_SPI_REG_CONFIG_TRM_RX;
369 if (transfer->bits_per_word)
370 word_len = transfer->bits_per_word;
371 sc.bitfield.wl = word_len - 1;
374 sc.bitfield.cs = spidev->chip_select;
376 /* ...and write the new settings */
377 kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
379 /* do the transfer */
380 count = kp_spi_txrx_pio(spidev, transfer);
381 m->actual_length += count;
383 if (count != transfer->len) {
389 if (transfer->delay_usecs)
390 udelay(transfer->delay_usecs);
393 /* de-assert chip select to end the sequence */
394 sc.reg = kp_spi_read_reg(cs, KP_SPI_REG_CONFIG);
395 sc.bitfield.spi_en = 0;
396 kp_spi_write_reg(cs, KP_SPI_REG_CONFIG, sc.reg);
400 spi_finalize_current_message(master);
409 kp_spi_cleanup(struct spi_device *spidev)
411 struct kp_spi_controller_state *cs = spidev->controller_state;
420 kp_spi_probe(struct platform_device *pldev)
422 struct kpc_core_device_platdata *drvdata;
423 struct spi_master *master;
424 struct kp_spi *kpspi;
429 drvdata = pldev->dev.platform_data;
431 dev_err(&pldev->dev, "%s: platform_data is NULL\n", __func__);
435 master = spi_alloc_master(&pldev->dev, sizeof(struct kp_spi));
437 dev_err(&pldev->dev, "%s: master allocation failed\n",
442 /* set up the spi functions */
443 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
444 master->bits_per_word_mask = (unsigned int)SPI_BPW_RANGE_MASK(4, 32);
445 master->setup = kp_spi_setup;
446 master->transfer_one_message = kp_spi_transfer_one_message;
447 master->cleanup = kp_spi_cleanup;
449 platform_set_drvdata(pldev, master);
451 kpspi = spi_master_get_devdata(master);
452 kpspi->master = master;
453 kpspi->dev = &pldev->dev;
455 master->num_chipselect = 4;
457 master->bus_num = pldev->id;
459 r = platform_get_resource(pldev, IORESOURCE_MEM, 0);
461 dev_err(&pldev->dev, "%s: Unable to get platform resources\n",
467 kpspi->base = devm_ioremap(&pldev->dev, r->start,
470 status = spi_register_master(master);
472 dev_err(&pldev->dev, "Unable to register SPI device\n");
476 /* register the slave boards */
477 #define NEW_SPI_DEVICE_FROM_BOARD_INFO_TABLE(table) \
478 for (i = 0 ; i < ARRAY_SIZE(table) ; i++) { \
479 spi_new_device(master, &table[i]); \
482 switch ((drvdata->card_id & 0xFFFF0000) >> 16) {
483 case PCI_DEVICE_ID_DAKTRONICS_KADOKA_P2KR0:
484 NEW_SPI_DEVICE_FROM_BOARD_INFO_TABLE(p2kr0_board_info);
487 dev_err(&pldev->dev, "Unknown hardware, cant know what partition table to use!\n");
494 spi_master_put(master);
499 kp_spi_remove(struct platform_device *pldev)
501 struct spi_master *master = platform_get_drvdata(pldev);
503 spi_unregister_master(master);
507 static struct platform_driver kp_spi_driver = {
509 .name = KP_DRIVER_NAME_SPI,
511 .probe = kp_spi_probe,
512 .remove = kp_spi_remove,
515 module_platform_driver(kp_spi_driver);
516 MODULE_LICENSE("GPL");
517 MODULE_ALIAS("platform:kp_spi");