spi-bcm-qspi.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  1. /*
  2. * Driver for Broadcom BRCMSTB, NSP, NS2, Cygnus SPI Controllers
  3. *
  4. * Copyright 2016 Broadcom
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation (the "GPL").
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License version 2 (GPLv2) for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * version 2 (GPLv2) along with this source code.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/delay.h>
  20. #include <linux/device.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/ioport.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/spi/spi-mem.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/types.h>
  35. #include "spi-bcm-qspi.h"
  36. #define DRIVER_NAME "bcm_qspi"
  37. /* BSPI register offsets */
  38. #define BSPI_REVISION_ID 0x000
  39. #define BSPI_SCRATCH 0x004
  40. #define BSPI_MAST_N_BOOT_CTRL 0x008
  41. #define BSPI_BUSY_STATUS 0x00c
  42. #define BSPI_INTR_STATUS 0x010
  43. #define BSPI_B0_STATUS 0x014
  44. #define BSPI_B0_CTRL 0x018
  45. #define BSPI_B1_STATUS 0x01c
  46. #define BSPI_B1_CTRL 0x020
  47. #define BSPI_STRAP_OVERRIDE_CTRL 0x024
  48. #define BSPI_FLEX_MODE_ENABLE 0x028
  49. #define BSPI_BITS_PER_CYCLE 0x02c
  50. #define BSPI_BITS_PER_PHASE 0x030
  51. #define BSPI_CMD_AND_MODE_BYTE 0x034
  52. #define BSPI_BSPI_FLASH_UPPER_ADDR_BYTE 0x038
  53. #define BSPI_BSPI_XOR_VALUE 0x03c
  54. #define BSPI_BSPI_XOR_ENABLE 0x040
  55. #define BSPI_BSPI_PIO_MODE_ENABLE 0x044
  56. #define BSPI_BSPI_PIO_IODIR 0x048
  57. #define BSPI_BSPI_PIO_DATA 0x04c
  58. /* RAF register offsets */
  59. #define BSPI_RAF_START_ADDR 0x100
  60. #define BSPI_RAF_NUM_WORDS 0x104
  61. #define BSPI_RAF_CTRL 0x108
  62. #define BSPI_RAF_FULLNESS 0x10c
  63. #define BSPI_RAF_WATERMARK 0x110
  64. #define BSPI_RAF_STATUS 0x114
  65. #define BSPI_RAF_READ_DATA 0x118
  66. #define BSPI_RAF_WORD_CNT 0x11c
  67. #define BSPI_RAF_CURR_ADDR 0x120
  68. /* Override mode masks */
  69. #define BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE BIT(0)
  70. #define BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL BIT(1)
  71. #define BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE BIT(2)
  72. #define BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD BIT(3)
  73. #define BSPI_STRAP_OVERRIDE_CTRL_ENDAIN_MODE BIT(4)
  74. #define BSPI_ADDRLEN_3BYTES 3
  75. #define BSPI_ADDRLEN_4BYTES 4
  76. #define BSPI_RAF_STATUS_FIFO_EMPTY_MASK BIT(1)
  77. #define BSPI_RAF_CTRL_START_MASK BIT(0)
  78. #define BSPI_RAF_CTRL_CLEAR_MASK BIT(1)
  79. #define BSPI_BPP_MODE_SELECT_MASK BIT(8)
  80. #define BSPI_BPP_ADDR_SELECT_MASK BIT(16)
  81. #define BSPI_READ_LENGTH 256
  82. /* MSPI register offsets */
  83. #define MSPI_SPCR0_LSB 0x000
  84. #define MSPI_SPCR0_MSB 0x004
  85. #define MSPI_SPCR1_LSB 0x008
  86. #define MSPI_SPCR1_MSB 0x00c
  87. #define MSPI_NEWQP 0x010
  88. #define MSPI_ENDQP 0x014
  89. #define MSPI_SPCR2 0x018
  90. #define MSPI_MSPI_STATUS 0x020
  91. #define MSPI_CPTQP 0x024
  92. #define MSPI_SPCR3 0x028
  93. #define MSPI_TXRAM 0x040
  94. #define MSPI_RXRAM 0x0c0
  95. #define MSPI_CDRAM 0x140
  96. #define MSPI_WRITE_LOCK 0x180
  97. #define MSPI_MASTER_BIT BIT(7)
  98. #define MSPI_NUM_CDRAM 16
  99. #define MSPI_CDRAM_CONT_BIT BIT(7)
  100. #define MSPI_CDRAM_BITSE_BIT BIT(6)
  101. #define MSPI_CDRAM_PCS 0xf
  102. #define MSPI_SPCR2_SPE BIT(6)
  103. #define MSPI_SPCR2_CONT_AFTER_CMD BIT(7)
  104. #define MSPI_MSPI_STATUS_SPIF BIT(0)
  105. #define INTR_BASE_BIT_SHIFT 0x02
  106. #define INTR_COUNT 0x07
  107. #define NUM_CHIPSELECT 4
  108. #define QSPI_SPBR_MIN 8U
  109. #define QSPI_SPBR_MAX 255U
  110. #define OPCODE_DIOR 0xBB
  111. #define OPCODE_QIOR 0xEB
  112. #define OPCODE_DIOR_4B 0xBC
  113. #define OPCODE_QIOR_4B 0xEC
  114. #define MAX_CMD_SIZE 6
  115. #define ADDR_4MB_MASK GENMASK(22, 0)
  116. /* stop at end of transfer, no other reason */
  117. #define TRANS_STATUS_BREAK_NONE 0
  118. /* stop at end of spi_message */
  119. #define TRANS_STATUS_BREAK_EOM 1
  120. /* stop at end of spi_transfer if delay */
  121. #define TRANS_STATUS_BREAK_DELAY 2
  122. /* stop at end of spi_transfer if cs_change */
  123. #define TRANS_STATUS_BREAK_CS_CHANGE 4
  124. /* stop if we run out of bytes */
  125. #define TRANS_STATUS_BREAK_NO_BYTES 8
  126. /* events that make us stop filling TX slots */
  127. #define TRANS_STATUS_BREAK_TX (TRANS_STATUS_BREAK_EOM | \
  128. TRANS_STATUS_BREAK_DELAY | \
  129. TRANS_STATUS_BREAK_CS_CHANGE)
  130. /* events that make us deassert CS */
  131. #define TRANS_STATUS_BREAK_DESELECT (TRANS_STATUS_BREAK_EOM | \
  132. TRANS_STATUS_BREAK_CS_CHANGE)
  133. struct bcm_qspi_parms {
  134. u32 speed_hz;
  135. u8 mode;
  136. u8 bits_per_word;
  137. };
  138. struct bcm_xfer_mode {
  139. bool flex_mode;
  140. unsigned int width;
  141. unsigned int addrlen;
  142. unsigned int hp;
  143. };
  144. enum base_type {
  145. MSPI,
  146. BSPI,
  147. CHIP_SELECT,
  148. BASEMAX,
  149. };
  150. enum irq_source {
  151. SINGLE_L2,
  152. MUXED_L1,
  153. };
  154. struct bcm_qspi_irq {
  155. const char *irq_name;
  156. const irq_handler_t irq_handler;
  157. int irq_source;
  158. u32 mask;
  159. };
  160. struct bcm_qspi_dev_id {
  161. const struct bcm_qspi_irq *irqp;
  162. void *dev;
  163. };
  164. struct qspi_trans {
  165. struct spi_transfer *trans;
  166. int byte;
  167. bool mspi_last_trans;
  168. };
  169. struct bcm_qspi {
  170. struct platform_device *pdev;
  171. struct spi_master *master;
  172. struct clk *clk;
  173. u32 base_clk;
  174. u32 max_speed_hz;
  175. void __iomem *base[BASEMAX];
  176. /* Some SoCs provide custom interrupt status register(s) */
  177. struct bcm_qspi_soc_intc *soc_intc;
  178. struct bcm_qspi_parms last_parms;
  179. struct qspi_trans trans_pos;
  180. int curr_cs;
  181. int bspi_maj_rev;
  182. int bspi_min_rev;
  183. int bspi_enabled;
  184. const struct spi_mem_op *bspi_rf_op;
  185. u32 bspi_rf_op_idx;
  186. u32 bspi_rf_op_len;
  187. u32 bspi_rf_op_status;
  188. struct bcm_xfer_mode xfer_mode;
  189. u32 s3_strap_override_ctrl;
  190. bool bspi_mode;
  191. bool big_endian;
  192. int num_irqs;
  193. struct bcm_qspi_dev_id *dev_ids;
  194. struct completion mspi_done;
  195. struct completion bspi_done;
  196. };
  197. static inline bool has_bspi(struct bcm_qspi *qspi)
  198. {
  199. return qspi->bspi_mode;
  200. }
  201. /* Read qspi controller register*/
  202. static inline u32 bcm_qspi_read(struct bcm_qspi *qspi, enum base_type type,
  203. unsigned int offset)
  204. {
  205. return bcm_qspi_readl(qspi->big_endian, qspi->base[type] + offset);
  206. }
  207. /* Write qspi controller register*/
  208. static inline void bcm_qspi_write(struct bcm_qspi *qspi, enum base_type type,
  209. unsigned int offset, unsigned int data)
  210. {
  211. bcm_qspi_writel(qspi->big_endian, data, qspi->base[type] + offset);
  212. }
  213. /* BSPI helpers */
  214. static int bcm_qspi_bspi_busy_poll(struct bcm_qspi *qspi)
  215. {
  216. int i;
  217. /* this should normally finish within 10us */
  218. for (i = 0; i < 1000; i++) {
  219. if (!(bcm_qspi_read(qspi, BSPI, BSPI_BUSY_STATUS) & 1))
  220. return 0;
  221. udelay(1);
  222. }
  223. dev_warn(&qspi->pdev->dev, "timeout waiting for !busy_status\n");
  224. return -EIO;
  225. }
  226. static inline bool bcm_qspi_bspi_ver_three(struct bcm_qspi *qspi)
  227. {
  228. if (qspi->bspi_maj_rev < 4)
  229. return true;
  230. return false;
  231. }
  232. static void bcm_qspi_bspi_flush_prefetch_buffers(struct bcm_qspi *qspi)
  233. {
  234. bcm_qspi_bspi_busy_poll(qspi);
  235. /* Force rising edge for the b0/b1 'flush' field */
  236. bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 1);
  237. bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 1);
  238. bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0);
  239. bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0);
  240. }
  241. static int bcm_qspi_bspi_lr_is_fifo_empty(struct bcm_qspi *qspi)
  242. {
  243. return (bcm_qspi_read(qspi, BSPI, BSPI_RAF_STATUS) &
  244. BSPI_RAF_STATUS_FIFO_EMPTY_MASK);
  245. }
  246. static inline u32 bcm_qspi_bspi_lr_read_fifo(struct bcm_qspi *qspi)
  247. {
  248. u32 data = bcm_qspi_read(qspi, BSPI, BSPI_RAF_READ_DATA);
  249. /* BSPI v3 LR is LE only, convert data to host endianness */
  250. if (bcm_qspi_bspi_ver_three(qspi))
  251. data = le32_to_cpu(data);
  252. return data;
  253. }
  254. static inline void bcm_qspi_bspi_lr_start(struct bcm_qspi *qspi)
  255. {
  256. bcm_qspi_bspi_busy_poll(qspi);
  257. bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL,
  258. BSPI_RAF_CTRL_START_MASK);
  259. }
  260. static inline void bcm_qspi_bspi_lr_clear(struct bcm_qspi *qspi)
  261. {
  262. bcm_qspi_write(qspi, BSPI, BSPI_RAF_CTRL,
  263. BSPI_RAF_CTRL_CLEAR_MASK);
  264. bcm_qspi_bspi_flush_prefetch_buffers(qspi);
  265. }
  266. static void bcm_qspi_bspi_lr_data_read(struct bcm_qspi *qspi)
  267. {
  268. u32 *buf = (u32 *)qspi->bspi_rf_op->data.buf.in;
  269. u32 data = 0;
  270. dev_dbg(&qspi->pdev->dev, "xfer %p rx %p rxlen %d\n", qspi->bspi_rf_op,
  271. qspi->bspi_rf_op->data.buf.in, qspi->bspi_rf_op_len);
  272. while (!bcm_qspi_bspi_lr_is_fifo_empty(qspi)) {
  273. data = bcm_qspi_bspi_lr_read_fifo(qspi);
  274. if (likely(qspi->bspi_rf_op_len >= 4) &&
  275. IS_ALIGNED((uintptr_t)buf, 4)) {
  276. buf[qspi->bspi_rf_op_idx++] = data;
  277. qspi->bspi_rf_op_len -= 4;
  278. } else {
  279. /* Read out remaining bytes, make sure*/
  280. u8 *cbuf = (u8 *)&buf[qspi->bspi_rf_op_idx];
  281. data = cpu_to_le32(data);
  282. while (qspi->bspi_rf_op_len) {
  283. *cbuf++ = (u8)data;
  284. data >>= 8;
  285. qspi->bspi_rf_op_len--;
  286. }
  287. }
  288. }
  289. }
  290. static void bcm_qspi_bspi_set_xfer_params(struct bcm_qspi *qspi, u8 cmd_byte,
  291. int bpp, int bpc, int flex_mode)
  292. {
  293. bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0);
  294. bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_CYCLE, bpc);
  295. bcm_qspi_write(qspi, BSPI, BSPI_BITS_PER_PHASE, bpp);
  296. bcm_qspi_write(qspi, BSPI, BSPI_CMD_AND_MODE_BYTE, cmd_byte);
  297. bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, flex_mode);
  298. }
  299. static int bcm_qspi_bspi_set_flex_mode(struct bcm_qspi *qspi,
  300. const struct spi_mem_op *op, int hp)
  301. {
  302. int bpc = 0, bpp = 0;
  303. u8 command = op->cmd.opcode;
  304. int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
  305. int addrlen = op->addr.nbytes;
  306. int flex_mode = 1;
  307. dev_dbg(&qspi->pdev->dev, "set flex mode w %x addrlen %x hp %d\n",
  308. width, addrlen, hp);
  309. if (addrlen == BSPI_ADDRLEN_4BYTES)
  310. bpp = BSPI_BPP_ADDR_SELECT_MASK;
  311. bpp |= (op->dummy.nbytes * 8) / op->dummy.buswidth;
  312. switch (width) {
  313. case SPI_NBITS_SINGLE:
  314. if (addrlen == BSPI_ADDRLEN_3BYTES)
  315. /* default mode, does not need flex_cmd */
  316. flex_mode = 0;
  317. break;
  318. case SPI_NBITS_DUAL:
  319. bpc = 0x00000001;
  320. if (hp) {
  321. bpc |= 0x00010100; /* address and mode are 2-bit */
  322. bpp = BSPI_BPP_MODE_SELECT_MASK;
  323. }
  324. break;
  325. case SPI_NBITS_QUAD:
  326. bpc = 0x00000002;
  327. if (hp) {
  328. bpc |= 0x00020200; /* address and mode are 4-bit */
  329. bpp |= BSPI_BPP_MODE_SELECT_MASK;
  330. }
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. bcm_qspi_bspi_set_xfer_params(qspi, command, bpp, bpc, flex_mode);
  336. return 0;
  337. }
  338. static int bcm_qspi_bspi_set_override(struct bcm_qspi *qspi,
  339. const struct spi_mem_op *op, int hp)
  340. {
  341. int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
  342. int addrlen = op->addr.nbytes;
  343. u32 data = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
  344. dev_dbg(&qspi->pdev->dev, "set override mode w %x addrlen %x hp %d\n",
  345. width, addrlen, hp);
  346. switch (width) {
  347. case SPI_NBITS_SINGLE:
  348. /* clear quad/dual mode */
  349. data &= ~(BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD |
  350. BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL);
  351. break;
  352. case SPI_NBITS_QUAD:
  353. /* clear dual mode and set quad mode */
  354. data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL;
  355. data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD;
  356. break;
  357. case SPI_NBITS_DUAL:
  358. /* clear quad mode set dual mode */
  359. data &= ~BSPI_STRAP_OVERRIDE_CTRL_DATA_QUAD;
  360. data |= BSPI_STRAP_OVERRIDE_CTRL_DATA_DUAL;
  361. break;
  362. default:
  363. return -EINVAL;
  364. }
  365. if (addrlen == BSPI_ADDRLEN_4BYTES)
  366. /* set 4byte mode*/
  367. data |= BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE;
  368. else
  369. /* clear 4 byte mode */
  370. data &= ~BSPI_STRAP_OVERRIDE_CTRL_ADDR_4BYTE;
  371. /* set the override mode */
  372. data |= BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE;
  373. bcm_qspi_write(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL, data);
  374. bcm_qspi_bspi_set_xfer_params(qspi, op->cmd.opcode, 0, 0, 0);
  375. return 0;
  376. }
  377. static int bcm_qspi_bspi_set_mode(struct bcm_qspi *qspi,
  378. const struct spi_mem_op *op, int hp)
  379. {
  380. int error = 0;
  381. int width = op->data.buswidth ? op->data.buswidth : SPI_NBITS_SINGLE;
  382. int addrlen = op->addr.nbytes;
  383. /* default mode */
  384. qspi->xfer_mode.flex_mode = true;
  385. if (!bcm_qspi_bspi_ver_three(qspi)) {
  386. u32 val, mask;
  387. val = bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
  388. mask = BSPI_STRAP_OVERRIDE_CTRL_OVERRIDE;
  389. if (val & mask || qspi->s3_strap_override_ctrl & mask) {
  390. qspi->xfer_mode.flex_mode = false;
  391. bcm_qspi_write(qspi, BSPI, BSPI_FLEX_MODE_ENABLE, 0);
  392. error = bcm_qspi_bspi_set_override(qspi, op, hp);
  393. }
  394. }
  395. if (qspi->xfer_mode.flex_mode)
  396. error = bcm_qspi_bspi_set_flex_mode(qspi, op, hp);
  397. if (error) {
  398. dev_warn(&qspi->pdev->dev,
  399. "INVALID COMBINATION: width=%d addrlen=%d hp=%d\n",
  400. width, addrlen, hp);
  401. } else if (qspi->xfer_mode.width != width ||
  402. qspi->xfer_mode.addrlen != addrlen ||
  403. qspi->xfer_mode.hp != hp) {
  404. qspi->xfer_mode.width = width;
  405. qspi->xfer_mode.addrlen = addrlen;
  406. qspi->xfer_mode.hp = hp;
  407. dev_dbg(&qspi->pdev->dev,
  408. "cs:%d %d-lane output, %d-byte address%s\n",
  409. qspi->curr_cs,
  410. qspi->xfer_mode.width,
  411. qspi->xfer_mode.addrlen,
  412. qspi->xfer_mode.hp != -1 ? ", hp mode" : "");
  413. }
  414. return error;
  415. }
  416. static void bcm_qspi_enable_bspi(struct bcm_qspi *qspi)
  417. {
  418. if (!has_bspi(qspi))
  419. return;
  420. qspi->bspi_enabled = 1;
  421. if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1) == 0)
  422. return;
  423. bcm_qspi_bspi_flush_prefetch_buffers(qspi);
  424. udelay(1);
  425. bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 0);
  426. udelay(1);
  427. }
  428. static void bcm_qspi_disable_bspi(struct bcm_qspi *qspi)
  429. {
  430. if (!has_bspi(qspi))
  431. return;
  432. qspi->bspi_enabled = 0;
  433. if ((bcm_qspi_read(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL) & 1))
  434. return;
  435. bcm_qspi_bspi_busy_poll(qspi);
  436. bcm_qspi_write(qspi, BSPI, BSPI_MAST_N_BOOT_CTRL, 1);
  437. udelay(1);
  438. }
  439. static void bcm_qspi_chip_select(struct bcm_qspi *qspi, int cs)
  440. {
  441. u32 rd = 0;
  442. u32 wr = 0;
  443. if (qspi->base[CHIP_SELECT]) {
  444. rd = bcm_qspi_read(qspi, CHIP_SELECT, 0);
  445. wr = (rd & ~0xff) | (1 << cs);
  446. if (rd == wr)
  447. return;
  448. bcm_qspi_write(qspi, CHIP_SELECT, 0, wr);
  449. usleep_range(10, 20);
  450. }
  451. dev_dbg(&qspi->pdev->dev, "using cs:%d\n", cs);
  452. qspi->curr_cs = cs;
  453. }
  454. /* MSPI helpers */
  455. static void bcm_qspi_hw_set_parms(struct bcm_qspi *qspi,
  456. const struct bcm_qspi_parms *xp)
  457. {
  458. u32 spcr, spbr = 0;
  459. if (xp->speed_hz)
  460. spbr = qspi->base_clk / (2 * xp->speed_hz);
  461. spcr = clamp_val(spbr, QSPI_SPBR_MIN, QSPI_SPBR_MAX);
  462. bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_LSB, spcr);
  463. spcr = MSPI_MASTER_BIT;
  464. /* for 16 bit the data should be zero */
  465. if (xp->bits_per_word != 16)
  466. spcr |= xp->bits_per_word << 2;
  467. spcr |= xp->mode & 3;
  468. bcm_qspi_write(qspi, MSPI, MSPI_SPCR0_MSB, spcr);
  469. qspi->last_parms = *xp;
  470. }
  471. static void bcm_qspi_update_parms(struct bcm_qspi *qspi,
  472. struct spi_device *spi,
  473. struct spi_transfer *trans)
  474. {
  475. struct bcm_qspi_parms xp;
  476. xp.speed_hz = trans->speed_hz;
  477. xp.bits_per_word = trans->bits_per_word;
  478. xp.mode = spi->mode;
  479. bcm_qspi_hw_set_parms(qspi, &xp);
  480. }
  481. static int bcm_qspi_setup(struct spi_device *spi)
  482. {
  483. struct bcm_qspi_parms *xp;
  484. if (spi->bits_per_word > 16)
  485. return -EINVAL;
  486. xp = spi_get_ctldata(spi);
  487. if (!xp) {
  488. xp = kzalloc(sizeof(*xp), GFP_KERNEL);
  489. if (!xp)
  490. return -ENOMEM;
  491. spi_set_ctldata(spi, xp);
  492. }
  493. xp->speed_hz = spi->max_speed_hz;
  494. xp->mode = spi->mode;
  495. if (spi->bits_per_word)
  496. xp->bits_per_word = spi->bits_per_word;
  497. else
  498. xp->bits_per_word = 8;
  499. return 0;
  500. }
  501. static bool bcm_qspi_mspi_transfer_is_last(struct bcm_qspi *qspi,
  502. struct qspi_trans *qt)
  503. {
  504. if (qt->mspi_last_trans &&
  505. spi_transfer_is_last(qspi->master, qt->trans))
  506. return true;
  507. else
  508. return false;
  509. }
  510. static int update_qspi_trans_byte_count(struct bcm_qspi *qspi,
  511. struct qspi_trans *qt, int flags)
  512. {
  513. int ret = TRANS_STATUS_BREAK_NONE;
  514. /* count the last transferred bytes */
  515. if (qt->trans->bits_per_word <= 8)
  516. qt->byte++;
  517. else
  518. qt->byte += 2;
  519. if (qt->byte >= qt->trans->len) {
  520. /* we're at the end of the spi_transfer */
  521. /* in TX mode, need to pause for a delay or CS change */
  522. if (qt->trans->delay_usecs &&
  523. (flags & TRANS_STATUS_BREAK_DELAY))
  524. ret |= TRANS_STATUS_BREAK_DELAY;
  525. if (qt->trans->cs_change &&
  526. (flags & TRANS_STATUS_BREAK_CS_CHANGE))
  527. ret |= TRANS_STATUS_BREAK_CS_CHANGE;
  528. if (ret)
  529. goto done;
  530. dev_dbg(&qspi->pdev->dev, "advance msg exit\n");
  531. if (bcm_qspi_mspi_transfer_is_last(qspi, qt))
  532. ret = TRANS_STATUS_BREAK_EOM;
  533. else
  534. ret = TRANS_STATUS_BREAK_NO_BYTES;
  535. qt->trans = NULL;
  536. }
  537. done:
  538. dev_dbg(&qspi->pdev->dev, "trans %p len %d byte %d ret %x\n",
  539. qt->trans, qt->trans ? qt->trans->len : 0, qt->byte, ret);
  540. return ret;
  541. }
  542. static inline u8 read_rxram_slot_u8(struct bcm_qspi *qspi, int slot)
  543. {
  544. u32 slot_offset = MSPI_RXRAM + (slot << 3) + 0x4;
  545. /* mask out reserved bits */
  546. return bcm_qspi_read(qspi, MSPI, slot_offset) & 0xff;
  547. }
  548. static inline u16 read_rxram_slot_u16(struct bcm_qspi *qspi, int slot)
  549. {
  550. u32 reg_offset = MSPI_RXRAM;
  551. u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
  552. u32 msb_offset = reg_offset + (slot << 3);
  553. return (bcm_qspi_read(qspi, MSPI, lsb_offset) & 0xff) |
  554. ((bcm_qspi_read(qspi, MSPI, msb_offset) & 0xff) << 8);
  555. }
  556. static void read_from_hw(struct bcm_qspi *qspi, int slots)
  557. {
  558. struct qspi_trans tp;
  559. int slot;
  560. bcm_qspi_disable_bspi(qspi);
  561. if (slots > MSPI_NUM_CDRAM) {
  562. /* should never happen */
  563. dev_err(&qspi->pdev->dev, "%s: too many slots!\n", __func__);
  564. return;
  565. }
  566. tp = qspi->trans_pos;
  567. for (slot = 0; slot < slots; slot++) {
  568. if (tp.trans->bits_per_word <= 8) {
  569. u8 *buf = tp.trans->rx_buf;
  570. if (buf)
  571. buf[tp.byte] = read_rxram_slot_u8(qspi, slot);
  572. dev_dbg(&qspi->pdev->dev, "RD %02x\n",
  573. buf ? buf[tp.byte] : 0xff);
  574. } else {
  575. u16 *buf = tp.trans->rx_buf;
  576. if (buf)
  577. buf[tp.byte / 2] = read_rxram_slot_u16(qspi,
  578. slot);
  579. dev_dbg(&qspi->pdev->dev, "RD %04x\n",
  580. buf ? buf[tp.byte] : 0xffff);
  581. }
  582. update_qspi_trans_byte_count(qspi, &tp,
  583. TRANS_STATUS_BREAK_NONE);
  584. }
  585. qspi->trans_pos = tp;
  586. }
  587. static inline void write_txram_slot_u8(struct bcm_qspi *qspi, int slot,
  588. u8 val)
  589. {
  590. u32 reg_offset = MSPI_TXRAM + (slot << 3);
  591. /* mask out reserved bits */
  592. bcm_qspi_write(qspi, MSPI, reg_offset, val);
  593. }
  594. static inline void write_txram_slot_u16(struct bcm_qspi *qspi, int slot,
  595. u16 val)
  596. {
  597. u32 reg_offset = MSPI_TXRAM;
  598. u32 msb_offset = reg_offset + (slot << 3);
  599. u32 lsb_offset = reg_offset + (slot << 3) + 0x4;
  600. bcm_qspi_write(qspi, MSPI, msb_offset, (val >> 8));
  601. bcm_qspi_write(qspi, MSPI, lsb_offset, (val & 0xff));
  602. }
  603. static inline u32 read_cdram_slot(struct bcm_qspi *qspi, int slot)
  604. {
  605. return bcm_qspi_read(qspi, MSPI, MSPI_CDRAM + (slot << 2));
  606. }
  607. static inline void write_cdram_slot(struct bcm_qspi *qspi, int slot, u32 val)
  608. {
  609. bcm_qspi_write(qspi, MSPI, (MSPI_CDRAM + (slot << 2)), val);
  610. }
  611. /* Return number of slots written */
  612. static int write_to_hw(struct bcm_qspi *qspi, struct spi_device *spi)
  613. {
  614. struct qspi_trans tp;
  615. int slot = 0, tstatus = 0;
  616. u32 mspi_cdram = 0;
  617. bcm_qspi_disable_bspi(qspi);
  618. tp = qspi->trans_pos;
  619. bcm_qspi_update_parms(qspi, spi, tp.trans);
  620. /* Run until end of transfer or reached the max data */
  621. while (!tstatus && slot < MSPI_NUM_CDRAM) {
  622. if (tp.trans->bits_per_word <= 8) {
  623. const u8 *buf = tp.trans->tx_buf;
  624. u8 val = buf ? buf[tp.byte] : 0xff;
  625. write_txram_slot_u8(qspi, slot, val);
  626. dev_dbg(&qspi->pdev->dev, "WR %02x\n", val);
  627. } else {
  628. const u16 *buf = tp.trans->tx_buf;
  629. u16 val = buf ? buf[tp.byte / 2] : 0xffff;
  630. write_txram_slot_u16(qspi, slot, val);
  631. dev_dbg(&qspi->pdev->dev, "WR %04x\n", val);
  632. }
  633. mspi_cdram = MSPI_CDRAM_CONT_BIT;
  634. if (has_bspi(qspi))
  635. mspi_cdram &= ~1;
  636. else
  637. mspi_cdram |= (~(1 << spi->chip_select) &
  638. MSPI_CDRAM_PCS);
  639. mspi_cdram |= ((tp.trans->bits_per_word <= 8) ? 0 :
  640. MSPI_CDRAM_BITSE_BIT);
  641. write_cdram_slot(qspi, slot, mspi_cdram);
  642. tstatus = update_qspi_trans_byte_count(qspi, &tp,
  643. TRANS_STATUS_BREAK_TX);
  644. slot++;
  645. }
  646. if (!slot) {
  647. dev_err(&qspi->pdev->dev, "%s: no data to send?", __func__);
  648. goto done;
  649. }
  650. dev_dbg(&qspi->pdev->dev, "submitting %d slots\n", slot);
  651. bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0);
  652. bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, slot - 1);
  653. if (tstatus & TRANS_STATUS_BREAK_DESELECT) {
  654. mspi_cdram = read_cdram_slot(qspi, slot - 1) &
  655. ~MSPI_CDRAM_CONT_BIT;
  656. write_cdram_slot(qspi, slot - 1, mspi_cdram);
  657. }
  658. if (has_bspi(qspi))
  659. bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 1);
  660. /* Must flush previous writes before starting MSPI operation */
  661. mb();
  662. /* Set cont | spe | spifie */
  663. bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0xe0);
  664. done:
  665. return slot;
  666. }
  667. static int bcm_qspi_bspi_exec_mem_op(struct spi_device *spi,
  668. const struct spi_mem_op *op)
  669. {
  670. struct bcm_qspi *qspi = spi_master_get_devdata(spi->master);
  671. u32 addr = 0, len, rdlen, len_words, from = 0;
  672. int ret = 0;
  673. unsigned long timeo = msecs_to_jiffies(100);
  674. struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
  675. if (bcm_qspi_bspi_ver_three(qspi))
  676. if (op->addr.nbytes == BSPI_ADDRLEN_4BYTES)
  677. return -EIO;
  678. from = op->addr.val;
  679. bcm_qspi_chip_select(qspi, spi->chip_select);
  680. bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
  681. /*
  682. * when using flex mode we need to send
  683. * the upper address byte to bspi
  684. */
  685. if (bcm_qspi_bspi_ver_three(qspi) == false) {
  686. addr = from & 0xff000000;
  687. bcm_qspi_write(qspi, BSPI,
  688. BSPI_BSPI_FLASH_UPPER_ADDR_BYTE, addr);
  689. }
  690. if (!qspi->xfer_mode.flex_mode)
  691. addr = from;
  692. else
  693. addr = from & 0x00ffffff;
  694. if (bcm_qspi_bspi_ver_three(qspi) == true)
  695. addr = (addr + 0xc00000) & 0xffffff;
  696. /*
  697. * read into the entire buffer by breaking the reads
  698. * into RAF buffer read lengths
  699. */
  700. len = op->data.nbytes;
  701. qspi->bspi_rf_op_idx = 0;
  702. do {
  703. if (len > BSPI_READ_LENGTH)
  704. rdlen = BSPI_READ_LENGTH;
  705. else
  706. rdlen = len;
  707. reinit_completion(&qspi->bspi_done);
  708. bcm_qspi_enable_bspi(qspi);
  709. len_words = (rdlen + 3) >> 2;
  710. qspi->bspi_rf_op = op;
  711. qspi->bspi_rf_op_status = 0;
  712. qspi->bspi_rf_op_len = rdlen;
  713. dev_dbg(&qspi->pdev->dev,
  714. "bspi xfr addr 0x%x len 0x%x", addr, rdlen);
  715. bcm_qspi_write(qspi, BSPI, BSPI_RAF_START_ADDR, addr);
  716. bcm_qspi_write(qspi, BSPI, BSPI_RAF_NUM_WORDS, len_words);
  717. bcm_qspi_write(qspi, BSPI, BSPI_RAF_WATERMARK, 0);
  718. if (qspi->soc_intc) {
  719. /*
  720. * clear soc MSPI and BSPI interrupts and enable
  721. * BSPI interrupts.
  722. */
  723. soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_BSPI_DONE);
  724. soc_intc->bcm_qspi_int_set(soc_intc, BSPI_DONE, true);
  725. }
  726. /* Must flush previous writes before starting BSPI operation */
  727. mb();
  728. bcm_qspi_bspi_lr_start(qspi);
  729. if (!wait_for_completion_timeout(&qspi->bspi_done, timeo)) {
  730. dev_err(&qspi->pdev->dev, "timeout waiting for BSPI\n");
  731. ret = -ETIMEDOUT;
  732. break;
  733. }
  734. /* set msg return length */
  735. addr += rdlen;
  736. len -= rdlen;
  737. } while (len);
  738. return ret;
  739. }
  740. static int bcm_qspi_transfer_one(struct spi_master *master,
  741. struct spi_device *spi,
  742. struct spi_transfer *trans)
  743. {
  744. struct bcm_qspi *qspi = spi_master_get_devdata(master);
  745. int slots;
  746. unsigned long timeo = msecs_to_jiffies(100);
  747. bcm_qspi_chip_select(qspi, spi->chip_select);
  748. qspi->trans_pos.trans = trans;
  749. qspi->trans_pos.byte = 0;
  750. while (qspi->trans_pos.byte < trans->len) {
  751. reinit_completion(&qspi->mspi_done);
  752. slots = write_to_hw(qspi, spi);
  753. if (!wait_for_completion_timeout(&qspi->mspi_done, timeo)) {
  754. dev_err(&qspi->pdev->dev, "timeout waiting for MSPI\n");
  755. return -ETIMEDOUT;
  756. }
  757. read_from_hw(qspi, slots);
  758. }
  759. return 0;
  760. }
  761. static int bcm_qspi_mspi_exec_mem_op(struct spi_device *spi,
  762. const struct spi_mem_op *op)
  763. {
  764. struct spi_master *master = spi->master;
  765. struct bcm_qspi *qspi = spi_master_get_devdata(master);
  766. struct spi_transfer t[2];
  767. u8 cmd[6] = { };
  768. int ret, i;
  769. memset(cmd, 0, sizeof(cmd));
  770. memset(t, 0, sizeof(t));
  771. /* tx */
  772. /* opcode is in cmd[0] */
  773. cmd[0] = op->cmd.opcode;
  774. for (i = 0; i < op->addr.nbytes; i++)
  775. cmd[1 + i] = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
  776. t[0].tx_buf = cmd;
  777. t[0].len = op->addr.nbytes + op->dummy.nbytes + 1;
  778. t[0].bits_per_word = spi->bits_per_word;
  779. t[0].tx_nbits = op->cmd.buswidth;
  780. /* lets mspi know that this is not last transfer */
  781. qspi->trans_pos.mspi_last_trans = false;
  782. ret = bcm_qspi_transfer_one(master, spi, &t[0]);
  783. /* rx */
  784. qspi->trans_pos.mspi_last_trans = true;
  785. if (!ret) {
  786. /* rx */
  787. t[1].rx_buf = op->data.buf.in;
  788. t[1].len = op->data.nbytes;
  789. t[1].rx_nbits = op->data.buswidth;
  790. t[1].bits_per_word = spi->bits_per_word;
  791. ret = bcm_qspi_transfer_one(master, spi, &t[1]);
  792. }
  793. return ret;
  794. }
  795. static int bcm_qspi_exec_mem_op(struct spi_mem *mem,
  796. const struct spi_mem_op *op)
  797. {
  798. struct spi_device *spi = mem->spi;
  799. struct bcm_qspi *qspi = spi_master_get_devdata(spi->master);
  800. int ret = 0;
  801. bool mspi_read = false;
  802. u32 addr = 0, len;
  803. u_char *buf;
  804. if (!op->data.nbytes || !op->addr.nbytes || op->addr.nbytes > 4 ||
  805. op->data.dir != SPI_MEM_DATA_IN)
  806. return -ENOTSUPP;
  807. buf = op->data.buf.in;
  808. addr = op->addr.val;
  809. len = op->data.nbytes;
  810. if (bcm_qspi_bspi_ver_three(qspi) == true) {
  811. /*
  812. * The address coming into this function is a raw flash offset.
  813. * But for BSPI <= V3, we need to convert it to a remapped BSPI
  814. * address. If it crosses a 4MB boundary, just revert back to
  815. * using MSPI.
  816. */
  817. addr = (addr + 0xc00000) & 0xffffff;
  818. if ((~ADDR_4MB_MASK & addr) ^
  819. (~ADDR_4MB_MASK & (addr + len - 1)))
  820. mspi_read = true;
  821. }
  822. /* non-aligned and very short transfers are handled by MSPI */
  823. if (!IS_ALIGNED((uintptr_t)addr, 4) || !IS_ALIGNED((uintptr_t)buf, 4) ||
  824. len < 4)
  825. mspi_read = true;
  826. if (mspi_read)
  827. return bcm_qspi_mspi_exec_mem_op(spi, op);
  828. ret = bcm_qspi_bspi_set_mode(qspi, op, 0);
  829. if (!ret)
  830. ret = bcm_qspi_bspi_exec_mem_op(spi, op);
  831. return ret;
  832. }
  833. static void bcm_qspi_cleanup(struct spi_device *spi)
  834. {
  835. struct bcm_qspi_parms *xp = spi_get_ctldata(spi);
  836. kfree(xp);
  837. }
  838. static irqreturn_t bcm_qspi_mspi_l2_isr(int irq, void *dev_id)
  839. {
  840. struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
  841. struct bcm_qspi *qspi = qspi_dev_id->dev;
  842. u32 status = bcm_qspi_read(qspi, MSPI, MSPI_MSPI_STATUS);
  843. if (status & MSPI_MSPI_STATUS_SPIF) {
  844. struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
  845. /* clear interrupt */
  846. status &= ~MSPI_MSPI_STATUS_SPIF;
  847. bcm_qspi_write(qspi, MSPI, MSPI_MSPI_STATUS, status);
  848. if (qspi->soc_intc)
  849. soc_intc->bcm_qspi_int_ack(soc_intc, MSPI_DONE);
  850. complete(&qspi->mspi_done);
  851. return IRQ_HANDLED;
  852. }
  853. return IRQ_NONE;
  854. }
  855. static irqreturn_t bcm_qspi_bspi_lr_l2_isr(int irq, void *dev_id)
  856. {
  857. struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
  858. struct bcm_qspi *qspi = qspi_dev_id->dev;
  859. struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
  860. u32 status = qspi_dev_id->irqp->mask;
  861. if (qspi->bspi_enabled && qspi->bspi_rf_op) {
  862. bcm_qspi_bspi_lr_data_read(qspi);
  863. if (qspi->bspi_rf_op_len == 0) {
  864. qspi->bspi_rf_op = NULL;
  865. if (qspi->soc_intc) {
  866. /* disable soc BSPI interrupt */
  867. soc_intc->bcm_qspi_int_set(soc_intc, BSPI_DONE,
  868. false);
  869. /* indicate done */
  870. status = INTR_BSPI_LR_SESSION_DONE_MASK;
  871. }
  872. if (qspi->bspi_rf_op_status)
  873. bcm_qspi_bspi_lr_clear(qspi);
  874. else
  875. bcm_qspi_bspi_flush_prefetch_buffers(qspi);
  876. }
  877. if (qspi->soc_intc)
  878. /* clear soc BSPI interrupt */
  879. soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_DONE);
  880. }
  881. status &= INTR_BSPI_LR_SESSION_DONE_MASK;
  882. if (qspi->bspi_enabled && status && qspi->bspi_rf_op_len == 0)
  883. complete(&qspi->bspi_done);
  884. return IRQ_HANDLED;
  885. }
  886. static irqreturn_t bcm_qspi_bspi_lr_err_l2_isr(int irq, void *dev_id)
  887. {
  888. struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
  889. struct bcm_qspi *qspi = qspi_dev_id->dev;
  890. struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
  891. dev_err(&qspi->pdev->dev, "BSPI INT error\n");
  892. qspi->bspi_rf_op_status = -EIO;
  893. if (qspi->soc_intc)
  894. /* clear soc interrupt */
  895. soc_intc->bcm_qspi_int_ack(soc_intc, BSPI_ERR);
  896. complete(&qspi->bspi_done);
  897. return IRQ_HANDLED;
  898. }
  899. static irqreturn_t bcm_qspi_l1_isr(int irq, void *dev_id)
  900. {
  901. struct bcm_qspi_dev_id *qspi_dev_id = dev_id;
  902. struct bcm_qspi *qspi = qspi_dev_id->dev;
  903. struct bcm_qspi_soc_intc *soc_intc = qspi->soc_intc;
  904. irqreturn_t ret = IRQ_NONE;
  905. if (soc_intc) {
  906. u32 status = soc_intc->bcm_qspi_get_int_status(soc_intc);
  907. if (status & MSPI_DONE)
  908. ret = bcm_qspi_mspi_l2_isr(irq, dev_id);
  909. else if (status & BSPI_DONE)
  910. ret = bcm_qspi_bspi_lr_l2_isr(irq, dev_id);
  911. else if (status & BSPI_ERR)
  912. ret = bcm_qspi_bspi_lr_err_l2_isr(irq, dev_id);
  913. }
  914. return ret;
  915. }
  916. static const struct bcm_qspi_irq qspi_irq_tab[] = {
  917. {
  918. .irq_name = "spi_lr_fullness_reached",
  919. .irq_handler = bcm_qspi_bspi_lr_l2_isr,
  920. .mask = INTR_BSPI_LR_FULLNESS_REACHED_MASK,
  921. },
  922. {
  923. .irq_name = "spi_lr_session_aborted",
  924. .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
  925. .mask = INTR_BSPI_LR_SESSION_ABORTED_MASK,
  926. },
  927. {
  928. .irq_name = "spi_lr_impatient",
  929. .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
  930. .mask = INTR_BSPI_LR_IMPATIENT_MASK,
  931. },
  932. {
  933. .irq_name = "spi_lr_session_done",
  934. .irq_handler = bcm_qspi_bspi_lr_l2_isr,
  935. .mask = INTR_BSPI_LR_SESSION_DONE_MASK,
  936. },
  937. #ifdef QSPI_INT_DEBUG
  938. /* this interrupt is for debug purposes only, dont request irq */
  939. {
  940. .irq_name = "spi_lr_overread",
  941. .irq_handler = bcm_qspi_bspi_lr_err_l2_isr,
  942. .mask = INTR_BSPI_LR_OVERREAD_MASK,
  943. },
  944. #endif
  945. {
  946. .irq_name = "mspi_done",
  947. .irq_handler = bcm_qspi_mspi_l2_isr,
  948. .mask = INTR_MSPI_DONE_MASK,
  949. },
  950. {
  951. .irq_name = "mspi_halted",
  952. .irq_handler = bcm_qspi_mspi_l2_isr,
  953. .mask = INTR_MSPI_HALTED_MASK,
  954. },
  955. {
  956. /* single muxed L1 interrupt source */
  957. .irq_name = "spi_l1_intr",
  958. .irq_handler = bcm_qspi_l1_isr,
  959. .irq_source = MUXED_L1,
  960. .mask = QSPI_INTERRUPTS_ALL,
  961. },
  962. };
  963. static void bcm_qspi_bspi_init(struct bcm_qspi *qspi)
  964. {
  965. u32 val = 0;
  966. val = bcm_qspi_read(qspi, BSPI, BSPI_REVISION_ID);
  967. qspi->bspi_maj_rev = (val >> 8) & 0xff;
  968. qspi->bspi_min_rev = val & 0xff;
  969. if (!(bcm_qspi_bspi_ver_three(qspi))) {
  970. /* Force mapping of BSPI address -> flash offset */
  971. bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_VALUE, 0);
  972. bcm_qspi_write(qspi, BSPI, BSPI_BSPI_XOR_ENABLE, 1);
  973. }
  974. qspi->bspi_enabled = 1;
  975. bcm_qspi_disable_bspi(qspi);
  976. bcm_qspi_write(qspi, BSPI, BSPI_B0_CTRL, 0);
  977. bcm_qspi_write(qspi, BSPI, BSPI_B1_CTRL, 0);
  978. }
  979. static void bcm_qspi_hw_init(struct bcm_qspi *qspi)
  980. {
  981. struct bcm_qspi_parms parms;
  982. bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_LSB, 0);
  983. bcm_qspi_write(qspi, MSPI, MSPI_SPCR1_MSB, 0);
  984. bcm_qspi_write(qspi, MSPI, MSPI_NEWQP, 0);
  985. bcm_qspi_write(qspi, MSPI, MSPI_ENDQP, 0);
  986. bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0x20);
  987. parms.mode = SPI_MODE_3;
  988. parms.bits_per_word = 8;
  989. parms.speed_hz = qspi->max_speed_hz;
  990. bcm_qspi_hw_set_parms(qspi, &parms);
  991. if (has_bspi(qspi))
  992. bcm_qspi_bspi_init(qspi);
  993. }
  994. static void bcm_qspi_hw_uninit(struct bcm_qspi *qspi)
  995. {
  996. bcm_qspi_write(qspi, MSPI, MSPI_SPCR2, 0);
  997. if (has_bspi(qspi))
  998. bcm_qspi_write(qspi, MSPI, MSPI_WRITE_LOCK, 0);
  999. }
  1000. static const struct spi_controller_mem_ops bcm_qspi_mem_ops = {
  1001. .exec_op = bcm_qspi_exec_mem_op,
  1002. };
  1003. static const struct of_device_id bcm_qspi_of_match[] = {
  1004. { .compatible = "brcm,spi-bcm-qspi" },
  1005. {},
  1006. };
  1007. MODULE_DEVICE_TABLE(of, bcm_qspi_of_match);
  1008. int bcm_qspi_probe(struct platform_device *pdev,
  1009. struct bcm_qspi_soc_intc *soc_intc)
  1010. {
  1011. struct device *dev = &pdev->dev;
  1012. struct bcm_qspi *qspi;
  1013. struct spi_master *master;
  1014. struct resource *res;
  1015. int irq, ret = 0, num_ints = 0;
  1016. u32 val;
  1017. const char *name = NULL;
  1018. int num_irqs = ARRAY_SIZE(qspi_irq_tab);
  1019. /* We only support device-tree instantiation */
  1020. if (!dev->of_node)
  1021. return -ENODEV;
  1022. if (!of_match_node(bcm_qspi_of_match, dev->of_node))
  1023. return -ENODEV;
  1024. master = spi_alloc_master(dev, sizeof(struct bcm_qspi));
  1025. if (!master) {
  1026. dev_err(dev, "error allocating spi_master\n");
  1027. return -ENOMEM;
  1028. }
  1029. qspi = spi_master_get_devdata(master);
  1030. qspi->pdev = pdev;
  1031. qspi->trans_pos.trans = NULL;
  1032. qspi->trans_pos.byte = 0;
  1033. qspi->trans_pos.mspi_last_trans = true;
  1034. qspi->master = master;
  1035. master->bus_num = -1;
  1036. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_RX_DUAL | SPI_RX_QUAD;
  1037. master->setup = bcm_qspi_setup;
  1038. master->transfer_one = bcm_qspi_transfer_one;
  1039. master->mem_ops = &bcm_qspi_mem_ops;
  1040. master->cleanup = bcm_qspi_cleanup;
  1041. master->dev.of_node = dev->of_node;
  1042. master->num_chipselect = NUM_CHIPSELECT;
  1043. qspi->big_endian = of_device_is_big_endian(dev->of_node);
  1044. if (!of_property_read_u32(dev->of_node, "num-cs", &val))
  1045. master->num_chipselect = val;
  1046. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hif_mspi");
  1047. if (!res)
  1048. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1049. "mspi");
  1050. if (res) {
  1051. qspi->base[MSPI] = devm_ioremap_resource(dev, res);
  1052. if (IS_ERR(qspi->base[MSPI])) {
  1053. ret = PTR_ERR(qspi->base[MSPI]);
  1054. goto qspi_resource_err;
  1055. }
  1056. } else {
  1057. goto qspi_resource_err;
  1058. }
  1059. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bspi");
  1060. if (res) {
  1061. qspi->base[BSPI] = devm_ioremap_resource(dev, res);
  1062. if (IS_ERR(qspi->base[BSPI])) {
  1063. ret = PTR_ERR(qspi->base[BSPI]);
  1064. goto qspi_resource_err;
  1065. }
  1066. qspi->bspi_mode = true;
  1067. } else {
  1068. qspi->bspi_mode = false;
  1069. }
  1070. dev_info(dev, "using %smspi mode\n", qspi->bspi_mode ? "bspi-" : "");
  1071. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs_reg");
  1072. if (res) {
  1073. qspi->base[CHIP_SELECT] = devm_ioremap_resource(dev, res);
  1074. if (IS_ERR(qspi->base[CHIP_SELECT])) {
  1075. ret = PTR_ERR(qspi->base[CHIP_SELECT]);
  1076. goto qspi_resource_err;
  1077. }
  1078. }
  1079. qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id),
  1080. GFP_KERNEL);
  1081. if (!qspi->dev_ids) {
  1082. ret = -ENOMEM;
  1083. goto qspi_resource_err;
  1084. }
  1085. for (val = 0; val < num_irqs; val++) {
  1086. irq = -1;
  1087. name = qspi_irq_tab[val].irq_name;
  1088. if (qspi_irq_tab[val].irq_source == SINGLE_L2) {
  1089. /* get the l2 interrupts */
  1090. irq = platform_get_irq_byname(pdev, name);
  1091. } else if (!num_ints && soc_intc) {
  1092. /* all mspi, bspi intrs muxed to one L1 intr */
  1093. irq = platform_get_irq(pdev, 0);
  1094. }
  1095. if (irq >= 0) {
  1096. ret = devm_request_irq(&pdev->dev, irq,
  1097. qspi_irq_tab[val].irq_handler, 0,
  1098. name,
  1099. &qspi->dev_ids[val]);
  1100. if (ret < 0) {
  1101. dev_err(&pdev->dev, "IRQ %s not found\n", name);
  1102. goto qspi_probe_err;
  1103. }
  1104. qspi->dev_ids[val].dev = qspi;
  1105. qspi->dev_ids[val].irqp = &qspi_irq_tab[val];
  1106. num_ints++;
  1107. dev_dbg(&pdev->dev, "registered IRQ %s %d\n",
  1108. qspi_irq_tab[val].irq_name,
  1109. irq);
  1110. }
  1111. }
  1112. if (!num_ints) {
  1113. dev_err(&pdev->dev, "no IRQs registered, cannot init driver\n");
  1114. ret = -EINVAL;
  1115. goto qspi_probe_err;
  1116. }
  1117. /*
  1118. * Some SoCs integrate spi controller (e.g., its interrupt bits)
  1119. * in specific ways
  1120. */
  1121. if (soc_intc) {
  1122. qspi->soc_intc = soc_intc;
  1123. soc_intc->bcm_qspi_int_set(soc_intc, MSPI_DONE, true);
  1124. } else {
  1125. qspi->soc_intc = NULL;
  1126. }
  1127. qspi->clk = devm_clk_get(&pdev->dev, NULL);
  1128. if (IS_ERR(qspi->clk)) {
  1129. dev_warn(dev, "unable to get clock\n");
  1130. ret = PTR_ERR(qspi->clk);
  1131. goto qspi_probe_err;
  1132. }
  1133. ret = clk_prepare_enable(qspi->clk);
  1134. if (ret) {
  1135. dev_err(dev, "failed to prepare clock\n");
  1136. goto qspi_probe_err;
  1137. }
  1138. qspi->base_clk = clk_get_rate(qspi->clk);
  1139. qspi->max_speed_hz = qspi->base_clk / (QSPI_SPBR_MIN * 2);
  1140. bcm_qspi_hw_init(qspi);
  1141. init_completion(&qspi->mspi_done);
  1142. init_completion(&qspi->bspi_done);
  1143. qspi->curr_cs = -1;
  1144. platform_set_drvdata(pdev, qspi);
  1145. qspi->xfer_mode.width = -1;
  1146. qspi->xfer_mode.addrlen = -1;
  1147. qspi->xfer_mode.hp = -1;
  1148. ret = devm_spi_register_master(&pdev->dev, master);
  1149. if (ret < 0) {
  1150. dev_err(dev, "can't register master\n");
  1151. goto qspi_reg_err;
  1152. }
  1153. return 0;
  1154. qspi_reg_err:
  1155. bcm_qspi_hw_uninit(qspi);
  1156. clk_disable_unprepare(qspi->clk);
  1157. qspi_probe_err:
  1158. kfree(qspi->dev_ids);
  1159. qspi_resource_err:
  1160. spi_master_put(master);
  1161. return ret;
  1162. }
  1163. /* probe function to be called by SoC specific platform driver probe */
  1164. EXPORT_SYMBOL_GPL(bcm_qspi_probe);
  1165. int bcm_qspi_remove(struct platform_device *pdev)
  1166. {
  1167. struct bcm_qspi *qspi = platform_get_drvdata(pdev);
  1168. bcm_qspi_hw_uninit(qspi);
  1169. clk_disable_unprepare(qspi->clk);
  1170. kfree(qspi->dev_ids);
  1171. spi_unregister_master(qspi->master);
  1172. return 0;
  1173. }
  1174. /* function to be called by SoC specific platform driver remove() */
  1175. EXPORT_SYMBOL_GPL(bcm_qspi_remove);
  1176. static int __maybe_unused bcm_qspi_suspend(struct device *dev)
  1177. {
  1178. struct bcm_qspi *qspi = dev_get_drvdata(dev);
  1179. /* store the override strap value */
  1180. if (!bcm_qspi_bspi_ver_three(qspi))
  1181. qspi->s3_strap_override_ctrl =
  1182. bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
  1183. spi_master_suspend(qspi->master);
  1184. clk_disable(qspi->clk);
  1185. bcm_qspi_hw_uninit(qspi);
  1186. return 0;
  1187. };
  1188. static int __maybe_unused bcm_qspi_resume(struct device *dev)
  1189. {
  1190. struct bcm_qspi *qspi = dev_get_drvdata(dev);
  1191. int ret = 0;
  1192. bcm_qspi_hw_init(qspi);
  1193. bcm_qspi_chip_select(qspi, qspi->curr_cs);
  1194. if (qspi->soc_intc)
  1195. /* enable MSPI interrupt */
  1196. qspi->soc_intc->bcm_qspi_int_set(qspi->soc_intc, MSPI_DONE,
  1197. true);
  1198. ret = clk_enable(qspi->clk);
  1199. if (!ret)
  1200. spi_master_resume(qspi->master);
  1201. return ret;
  1202. }
  1203. SIMPLE_DEV_PM_OPS(bcm_qspi_pm_ops, bcm_qspi_suspend, bcm_qspi_resume);
  1204. /* pm_ops to be called by SoC specific platform driver */
  1205. EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops);
  1206. MODULE_AUTHOR("Kamal Dasu");
  1207. MODULE_DESCRIPTION("Broadcom QSPI driver");
  1208. MODULE_LICENSE("GPL v2");
  1209. MODULE_ALIAS("platform:" DRIVER_NAME);