spi-bcm-qspi.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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] : 0x0);
  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 / 2] : 0x0);
  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] : 0x00;
  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] : 0x0000;
  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 = devm_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. return PTR_ERR(qspi->base[MSPI]);
  1054. } else {
  1055. return 0;
  1056. }
  1057. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "bspi");
  1058. if (res) {
  1059. qspi->base[BSPI] = devm_ioremap_resource(dev, res);
  1060. if (IS_ERR(qspi->base[BSPI]))
  1061. return PTR_ERR(qspi->base[BSPI]);
  1062. qspi->bspi_mode = true;
  1063. } else {
  1064. qspi->bspi_mode = false;
  1065. }
  1066. dev_info(dev, "using %smspi mode\n", qspi->bspi_mode ? "bspi-" : "");
  1067. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs_reg");
  1068. if (res) {
  1069. qspi->base[CHIP_SELECT] = devm_ioremap_resource(dev, res);
  1070. if (IS_ERR(qspi->base[CHIP_SELECT]))
  1071. return PTR_ERR(qspi->base[CHIP_SELECT]);
  1072. }
  1073. qspi->dev_ids = kcalloc(num_irqs, sizeof(struct bcm_qspi_dev_id),
  1074. GFP_KERNEL);
  1075. if (!qspi->dev_ids)
  1076. return -ENOMEM;
  1077. for (val = 0; val < num_irqs; val++) {
  1078. irq = -1;
  1079. name = qspi_irq_tab[val].irq_name;
  1080. if (qspi_irq_tab[val].irq_source == SINGLE_L2) {
  1081. /* get the l2 interrupts */
  1082. irq = platform_get_irq_byname(pdev, name);
  1083. } else if (!num_ints && soc_intc) {
  1084. /* all mspi, bspi intrs muxed to one L1 intr */
  1085. irq = platform_get_irq(pdev, 0);
  1086. }
  1087. if (irq >= 0) {
  1088. ret = devm_request_irq(&pdev->dev, irq,
  1089. qspi_irq_tab[val].irq_handler, 0,
  1090. name,
  1091. &qspi->dev_ids[val]);
  1092. if (ret < 0) {
  1093. dev_err(&pdev->dev, "IRQ %s not found\n", name);
  1094. goto qspi_probe_err;
  1095. }
  1096. qspi->dev_ids[val].dev = qspi;
  1097. qspi->dev_ids[val].irqp = &qspi_irq_tab[val];
  1098. num_ints++;
  1099. dev_dbg(&pdev->dev, "registered IRQ %s %d\n",
  1100. qspi_irq_tab[val].irq_name,
  1101. irq);
  1102. }
  1103. }
  1104. if (!num_ints) {
  1105. dev_err(&pdev->dev, "no IRQs registered, cannot init driver\n");
  1106. ret = -EINVAL;
  1107. goto qspi_probe_err;
  1108. }
  1109. /*
  1110. * Some SoCs integrate spi controller (e.g., its interrupt bits)
  1111. * in specific ways
  1112. */
  1113. if (soc_intc) {
  1114. qspi->soc_intc = soc_intc;
  1115. soc_intc->bcm_qspi_int_set(soc_intc, MSPI_DONE, true);
  1116. } else {
  1117. qspi->soc_intc = NULL;
  1118. }
  1119. qspi->clk = devm_clk_get(&pdev->dev, NULL);
  1120. if (IS_ERR(qspi->clk)) {
  1121. dev_warn(dev, "unable to get clock\n");
  1122. ret = PTR_ERR(qspi->clk);
  1123. goto qspi_probe_err;
  1124. }
  1125. ret = clk_prepare_enable(qspi->clk);
  1126. if (ret) {
  1127. dev_err(dev, "failed to prepare clock\n");
  1128. goto qspi_probe_err;
  1129. }
  1130. qspi->base_clk = clk_get_rate(qspi->clk);
  1131. qspi->max_speed_hz = qspi->base_clk / (QSPI_SPBR_MIN * 2);
  1132. bcm_qspi_hw_init(qspi);
  1133. init_completion(&qspi->mspi_done);
  1134. init_completion(&qspi->bspi_done);
  1135. qspi->curr_cs = -1;
  1136. platform_set_drvdata(pdev, qspi);
  1137. qspi->xfer_mode.width = -1;
  1138. qspi->xfer_mode.addrlen = -1;
  1139. qspi->xfer_mode.hp = -1;
  1140. ret = spi_register_master(master);
  1141. if (ret < 0) {
  1142. dev_err(dev, "can't register master\n");
  1143. goto qspi_reg_err;
  1144. }
  1145. return 0;
  1146. qspi_reg_err:
  1147. bcm_qspi_hw_uninit(qspi);
  1148. clk_disable_unprepare(qspi->clk);
  1149. qspi_probe_err:
  1150. kfree(qspi->dev_ids);
  1151. return ret;
  1152. }
  1153. /* probe function to be called by SoC specific platform driver probe */
  1154. EXPORT_SYMBOL_GPL(bcm_qspi_probe);
  1155. int bcm_qspi_remove(struct platform_device *pdev)
  1156. {
  1157. struct bcm_qspi *qspi = platform_get_drvdata(pdev);
  1158. spi_unregister_master(qspi->master);
  1159. bcm_qspi_hw_uninit(qspi);
  1160. clk_disable_unprepare(qspi->clk);
  1161. kfree(qspi->dev_ids);
  1162. return 0;
  1163. }
  1164. /* function to be called by SoC specific platform driver remove() */
  1165. EXPORT_SYMBOL_GPL(bcm_qspi_remove);
  1166. static int __maybe_unused bcm_qspi_suspend(struct device *dev)
  1167. {
  1168. struct bcm_qspi *qspi = dev_get_drvdata(dev);
  1169. /* store the override strap value */
  1170. if (!bcm_qspi_bspi_ver_three(qspi))
  1171. qspi->s3_strap_override_ctrl =
  1172. bcm_qspi_read(qspi, BSPI, BSPI_STRAP_OVERRIDE_CTRL);
  1173. spi_master_suspend(qspi->master);
  1174. clk_disable(qspi->clk);
  1175. bcm_qspi_hw_uninit(qspi);
  1176. return 0;
  1177. };
  1178. static int __maybe_unused bcm_qspi_resume(struct device *dev)
  1179. {
  1180. struct bcm_qspi *qspi = dev_get_drvdata(dev);
  1181. int ret = 0;
  1182. bcm_qspi_hw_init(qspi);
  1183. bcm_qspi_chip_select(qspi, qspi->curr_cs);
  1184. if (qspi->soc_intc)
  1185. /* enable MSPI interrupt */
  1186. qspi->soc_intc->bcm_qspi_int_set(qspi->soc_intc, MSPI_DONE,
  1187. true);
  1188. ret = clk_enable(qspi->clk);
  1189. if (!ret)
  1190. spi_master_resume(qspi->master);
  1191. return ret;
  1192. }
  1193. SIMPLE_DEV_PM_OPS(bcm_qspi_pm_ops, bcm_qspi_suspend, bcm_qspi_resume);
  1194. /* pm_ops to be called by SoC specific platform driver */
  1195. EXPORT_SYMBOL_GPL(bcm_qspi_pm_ops);
  1196. MODULE_AUTHOR("Kamal Dasu");
  1197. MODULE_DESCRIPTION("Broadcom QSPI driver");
  1198. MODULE_LICENSE("GPL v2");
  1199. MODULE_ALIAS("platform:" DRIVER_NAME);