spi-bcm-qspi.c 35 KB

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