bcm63xx_hsspi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
  4. *
  5. * Derived from linux/drivers/spi/spi-bcm63xx-hsspi.c:
  6. * Copyright (C) 2000-2010 Broadcom Corporation
  7. * Copyright (C) 2012-2013 Jonas Gorski <jogo@openwrt.org>
  8. */
  9. #include <common.h>
  10. #include <clk.h>
  11. #include <dm.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <spi.h>
  15. #include <reset.h>
  16. #include <wait_bit.h>
  17. #include <asm/io.h>
  18. #include <linux/bitops.h>
  19. #define HSSPI_PP 0
  20. /*
  21. * The maximum frequency for SPI synchronous mode is 30MHz for some chips and
  22. * 25MHz for some others. This depends on the chip layout and SPI signals
  23. * distance to the pad. We use the lower of these values to cover all relevant
  24. * chips.
  25. */
  26. #define SPI_MAX_SYNC_CLOCK 25000000
  27. /* SPI Control register */
  28. #define SPI_CTL_REG 0x000
  29. #define SPI_CTL_CS_POL_SHIFT 0
  30. #define SPI_CTL_CS_POL_MASK (0xff << SPI_CTL_CS_POL_SHIFT)
  31. #define SPI_CTL_CLK_GATE_SHIFT 16
  32. #define SPI_CTL_CLK_GATE_MASK (1 << SPI_CTL_CLK_GATE_SHIFT)
  33. #define SPI_CTL_CLK_POL_SHIFT 17
  34. #define SPI_CTL_CLK_POL_MASK (1 << SPI_CTL_CLK_POL_SHIFT)
  35. /* SPI Interrupts registers */
  36. #define SPI_IR_STAT_REG 0x008
  37. #define SPI_IR_ST_MASK_REG 0x00c
  38. #define SPI_IR_MASK_REG 0x010
  39. #define SPI_IR_CLEAR_ALL 0xff001f1f
  40. /* SPI Ping-Pong Command registers */
  41. #define SPI_CMD_REG (0x080 + (0x40 * (HSSPI_PP)) + 0x00)
  42. #define SPI_CMD_OP_SHIFT 0
  43. #define SPI_CMD_OP_START (0x1 << SPI_CMD_OP_SHIFT)
  44. #define SPI_CMD_PFL_SHIFT 8
  45. #define SPI_CMD_PFL_MASK (0x7 << SPI_CMD_PFL_SHIFT)
  46. #define SPI_CMD_SLAVE_SHIFT 12
  47. #define SPI_CMD_SLAVE_MASK (0x7 << SPI_CMD_SLAVE_SHIFT)
  48. /* SPI Ping-Pong Status registers */
  49. #define SPI_STAT_REG (0x080 + (0x40 * (HSSPI_PP)) + 0x04)
  50. #define SPI_STAT_SRCBUSY_SHIFT 1
  51. #define SPI_STAT_SRCBUSY_MASK (1 << SPI_STAT_SRCBUSY_SHIFT)
  52. /* SPI Profile Clock registers */
  53. #define SPI_PFL_CLK_REG(x) (0x100 + (0x20 * (x)) + 0x00)
  54. #define SPI_PFL_CLK_FREQ_SHIFT 0
  55. #define SPI_PFL_CLK_FREQ_MASK (0x3fff << SPI_PFL_CLK_FREQ_SHIFT)
  56. #define SPI_PFL_CLK_RSTLOOP_SHIFT 15
  57. #define SPI_PFL_CLK_RSTLOOP_MASK (1 << SPI_PFL_CLK_RSTLOOP_SHIFT)
  58. /* SPI Profile Signal registers */
  59. #define SPI_PFL_SIG_REG(x) (0x100 + (0x20 * (x)) + 0x04)
  60. #define SPI_PFL_SIG_LATCHRIS_SHIFT 12
  61. #define SPI_PFL_SIG_LATCHRIS_MASK (1 << SPI_PFL_SIG_LATCHRIS_SHIFT)
  62. #define SPI_PFL_SIG_LAUNCHRIS_SHIFT 13
  63. #define SPI_PFL_SIG_LAUNCHRIS_MASK (1 << SPI_PFL_SIG_LAUNCHRIS_SHIFT)
  64. #define SPI_PFL_SIG_ASYNCIN_SHIFT 16
  65. #define SPI_PFL_SIG_ASYNCIN_MASK (1 << SPI_PFL_SIG_ASYNCIN_SHIFT)
  66. /* SPI Profile Mode registers */
  67. #define SPI_PFL_MODE_REG(x) (0x100 + (0x20 * (x)) + 0x08)
  68. #define SPI_PFL_MODE_FILL_SHIFT 0
  69. #define SPI_PFL_MODE_FILL_MASK (0xff << SPI_PFL_MODE_FILL_SHIFT)
  70. #define SPI_PFL_MODE_MDRDST_SHIFT 8
  71. #define SPI_PFL_MODE_MDWRST_SHIFT 12
  72. #define SPI_PFL_MODE_MDRDSZ_SHIFT 16
  73. #define SPI_PFL_MODE_MDRDSZ_MASK (1 << SPI_PFL_MODE_MDRDSZ_SHIFT)
  74. #define SPI_PFL_MODE_MDWRSZ_SHIFT 18
  75. #define SPI_PFL_MODE_MDWRSZ_MASK (1 << SPI_PFL_MODE_MDWRSZ_SHIFT)
  76. #define SPI_PFL_MODE_3WIRE_SHIFT 20
  77. #define SPI_PFL_MODE_3WIRE_MASK (1 << SPI_PFL_MODE_3WIRE_SHIFT)
  78. #define SPI_PFL_MODE_PREPCNT_SHIFT 24
  79. #define SPI_PFL_MODE_PREPCNT_MASK (4 << SPI_PFL_MODE_PREPCNT_SHIFT)
  80. /* SPI Ping-Pong FIFO registers */
  81. #define HSSPI_FIFO_SIZE 0x200
  82. #define HSSPI_FIFO_BASE (0x200 + \
  83. (HSSPI_FIFO_SIZE * HSSPI_PP))
  84. /* SPI Ping-Pong FIFO OP register */
  85. #define HSSPI_FIFO_OP_SIZE 0x2
  86. #define HSSPI_FIFO_OP_REG (HSSPI_FIFO_BASE + 0x00)
  87. #define HSSPI_FIFO_OP_BYTES_SHIFT 0
  88. #define HSSPI_FIFO_OP_BYTES_MASK (0x3ff << HSSPI_FIFO_OP_BYTES_SHIFT)
  89. #define HSSPI_FIFO_OP_MBIT_SHIFT 11
  90. #define HSSPI_FIFO_OP_MBIT_MASK (1 << HSSPI_FIFO_OP_MBIT_SHIFT)
  91. #define HSSPI_FIFO_OP_CODE_SHIFT 13
  92. #define HSSPI_FIFO_OP_READ_WRITE (1 << HSSPI_FIFO_OP_CODE_SHIFT)
  93. #define HSSPI_FIFO_OP_CODE_W (2 << HSSPI_FIFO_OP_CODE_SHIFT)
  94. #define HSSPI_FIFO_OP_CODE_R (3 << HSSPI_FIFO_OP_CODE_SHIFT)
  95. #define HSSPI_MAX_DATA_SIZE (HSSPI_FIFO_SIZE - HSSPI_FIFO_OP_SIZE)
  96. #define HSSPI_MAX_PREPEND_SIZE 15
  97. #define HSSPI_XFER_MODE_PREPEND 0
  98. #define HSSPI_XFER_MODE_DUMMYCS 1
  99. struct bcm63xx_hsspi_priv {
  100. void __iomem *regs;
  101. ulong clk_rate;
  102. uint8_t num_cs;
  103. uint8_t cs_pols;
  104. uint speed;
  105. uint xfer_mode;
  106. uint32_t prepend_cnt;
  107. uint8_t prepend_buf[HSSPI_MAX_PREPEND_SIZE];
  108. };
  109. static int bcm63xx_hsspi_cs_info(struct udevice *bus, uint cs,
  110. struct spi_cs_info *info)
  111. {
  112. struct bcm63xx_hsspi_priv *priv = dev_get_priv(bus);
  113. if (cs >= priv->num_cs) {
  114. printf("no cs %u\n", cs);
  115. return -EINVAL;
  116. }
  117. return 0;
  118. }
  119. static int bcm63xx_hsspi_set_mode(struct udevice *bus, uint mode)
  120. {
  121. struct bcm63xx_hsspi_priv *priv = dev_get_priv(bus);
  122. /* clock polarity */
  123. if (mode & SPI_CPOL)
  124. setbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_POL_MASK);
  125. else
  126. clrbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_POL_MASK);
  127. return 0;
  128. }
  129. static int bcm63xx_hsspi_set_speed(struct udevice *bus, uint speed)
  130. {
  131. struct bcm63xx_hsspi_priv *priv = dev_get_priv(bus);
  132. priv->speed = speed;
  133. return 0;
  134. }
  135. static void bcm63xx_hsspi_activate_cs(struct bcm63xx_hsspi_priv *priv,
  136. struct dm_spi_slave_plat *plat)
  137. {
  138. uint32_t clr, set;
  139. uint speed = priv->speed;
  140. if (priv->xfer_mode == HSSPI_XFER_MODE_DUMMYCS &&
  141. speed > SPI_MAX_SYNC_CLOCK) {
  142. speed = SPI_MAX_SYNC_CLOCK;
  143. debug("Force to dummy cs mode. Reduce the speed to %dHz\n", speed);
  144. }
  145. /* profile clock */
  146. set = DIV_ROUND_UP(priv->clk_rate, speed);
  147. set = DIV_ROUND_UP(2048, set);
  148. set &= SPI_PFL_CLK_FREQ_MASK;
  149. set |= SPI_PFL_CLK_RSTLOOP_MASK;
  150. writel(set, priv->regs + SPI_PFL_CLK_REG(plat->cs));
  151. /* profile signal */
  152. set = 0;
  153. clr = SPI_PFL_SIG_LAUNCHRIS_MASK |
  154. SPI_PFL_SIG_LATCHRIS_MASK |
  155. SPI_PFL_SIG_ASYNCIN_MASK;
  156. /* latch/launch config */
  157. if (plat->mode & SPI_CPHA)
  158. set |= SPI_PFL_SIG_LAUNCHRIS_MASK;
  159. else
  160. set |= SPI_PFL_SIG_LATCHRIS_MASK;
  161. /* async clk */
  162. if (speed > SPI_MAX_SYNC_CLOCK)
  163. set |= SPI_PFL_SIG_ASYNCIN_MASK;
  164. clrsetbits_32(priv->regs + SPI_PFL_SIG_REG(plat->cs), clr, set);
  165. /* global control */
  166. set = 0;
  167. clr = 0;
  168. if (priv->xfer_mode == HSSPI_XFER_MODE_PREPEND) {
  169. if (priv->cs_pols & BIT(plat->cs))
  170. set |= BIT(plat->cs);
  171. else
  172. clr |= BIT(plat->cs);
  173. } else {
  174. /* invert cs polarity */
  175. if (priv->cs_pols & BIT(plat->cs))
  176. clr |= BIT(plat->cs);
  177. else
  178. set |= BIT(plat->cs);
  179. /* invert dummy cs polarity */
  180. if (priv->cs_pols & BIT(!plat->cs))
  181. clr |= BIT(!plat->cs);
  182. else
  183. set |= BIT(!plat->cs);
  184. }
  185. clrsetbits_32(priv->regs + SPI_CTL_REG, clr, set);
  186. }
  187. static void bcm63xx_hsspi_deactivate_cs(struct bcm63xx_hsspi_priv *priv)
  188. {
  189. /* restore cs polarities */
  190. clrsetbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CS_POL_MASK,
  191. priv->cs_pols);
  192. }
  193. /*
  194. * BCM63xx HSSPI driver doesn't allow keeping CS active between transfers
  195. * because they are controlled by HW.
  196. * However, it provides a mechanism to prepend write transfers prior to read
  197. * transfers (with a maximum prepend of 15 bytes), which is usually enough for
  198. * SPI-connected flashes since reading requires prepending a write transfer of
  199. * 5 bytes. On the other hand it also provides a way to invert each CS
  200. * polarity, not only between transfers like the older BCM63xx SPI driver, but
  201. * also the rest of the time.
  202. *
  203. * Instead of using the prepend mechanism, this implementation inverts the
  204. * polarity of both the desired CS and another dummy CS when the bus is
  205. * claimed. This way, the dummy CS is restored to its inactive value when
  206. * transfers are issued and the desired CS is preserved in its active value
  207. * all the time. This hack is also used in the upstream linux driver and
  208. * allows keeping CS active between transfers even if the HW doesn't give
  209. * this possibility.
  210. *
  211. * This workaround only works when the dummy CS (usually CS1 when the actual
  212. * CS is 0) pinmuxed to SPI chip select function if SPI clock is faster than
  213. * SPI_MAX_SYNC_CLOCK. In old broadcom chip, CS1 pin is default to chip select
  214. * function. But this is not the case for new chips. To make this function
  215. * always work, it should be called with maximum clock of SPI_MAX_SYNC_CLOCK.
  216. */
  217. static int bcm63xx_hsspi_xfer_dummy_cs(struct udevice *dev, unsigned int data_bytes,
  218. const void *dout, void *din, unsigned long flags)
  219. {
  220. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev->parent);
  221. struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
  222. size_t step_size = HSSPI_FIFO_SIZE;
  223. uint16_t opcode = 0;
  224. uint32_t val = SPI_PFL_MODE_FILL_MASK;
  225. const uint8_t *tx = dout;
  226. uint8_t *rx = din;
  227. if (flags & SPI_XFER_BEGIN)
  228. bcm63xx_hsspi_activate_cs(priv, plat);
  229. /* fifo operation */
  230. if (tx && rx)
  231. opcode = HSSPI_FIFO_OP_READ_WRITE;
  232. else if (rx)
  233. opcode = HSSPI_FIFO_OP_CODE_R;
  234. else if (tx)
  235. opcode = HSSPI_FIFO_OP_CODE_W;
  236. if (opcode != HSSPI_FIFO_OP_CODE_R)
  237. step_size -= HSSPI_FIFO_OP_SIZE;
  238. /* dual mode */
  239. if ((opcode == HSSPI_FIFO_OP_CODE_R && (plat->mode & SPI_RX_DUAL)) ||
  240. (opcode == HSSPI_FIFO_OP_CODE_W && (plat->mode & SPI_TX_DUAL))) {
  241. opcode |= HSSPI_FIFO_OP_MBIT_MASK;
  242. /* profile mode */
  243. if (plat->mode & SPI_RX_DUAL)
  244. val |= SPI_PFL_MODE_MDRDSZ_MASK;
  245. if (plat->mode & SPI_TX_DUAL)
  246. val |= SPI_PFL_MODE_MDWRSZ_MASK;
  247. }
  248. if (plat->mode & SPI_3WIRE)
  249. val |= SPI_PFL_MODE_3WIRE_MASK;
  250. writel(val, priv->regs + SPI_PFL_MODE_REG(plat->cs));
  251. /* transfer loop */
  252. while (data_bytes > 0) {
  253. size_t curr_step = min(step_size, data_bytes);
  254. int ret;
  255. /* copy tx data */
  256. if (tx) {
  257. memcpy_toio(priv->regs + HSSPI_FIFO_BASE +
  258. HSSPI_FIFO_OP_SIZE, tx, curr_step);
  259. tx += curr_step;
  260. }
  261. /* set fifo operation */
  262. writew(cpu_to_be16(opcode | (curr_step & HSSPI_FIFO_OP_BYTES_MASK)),
  263. priv->regs + HSSPI_FIFO_OP_REG);
  264. /* issue the transfer */
  265. val = SPI_CMD_OP_START;
  266. val |= (plat->cs << SPI_CMD_PFL_SHIFT) &
  267. SPI_CMD_PFL_MASK;
  268. val |= (!plat->cs << SPI_CMD_SLAVE_SHIFT) &
  269. SPI_CMD_SLAVE_MASK;
  270. writel(val, priv->regs + SPI_CMD_REG);
  271. /* wait for completion */
  272. ret = wait_for_bit_32(priv->regs + SPI_STAT_REG,
  273. SPI_STAT_SRCBUSY_MASK, false,
  274. 1000, false);
  275. if (ret) {
  276. printf("interrupt timeout\n");
  277. return ret;
  278. }
  279. /* copy rx data */
  280. if (rx) {
  281. memcpy_fromio(rx, priv->regs + HSSPI_FIFO_BASE,
  282. curr_step);
  283. rx += curr_step;
  284. }
  285. data_bytes -= curr_step;
  286. }
  287. if (flags & SPI_XFER_END)
  288. bcm63xx_hsspi_deactivate_cs(priv);
  289. return 0;
  290. }
  291. static int bcm63xx_prepare_prepend_transfer(struct bcm63xx_hsspi_priv *priv,
  292. unsigned int data_bytes, const void *dout, void *din,
  293. unsigned long flags)
  294. {
  295. /*
  296. * only support multiple half duplex write transfer + optional
  297. * full duplex read/write at the end.
  298. */
  299. if (flags & SPI_XFER_BEGIN) {
  300. /* clear prepends */
  301. priv->prepend_cnt = 0;
  302. }
  303. if (din) {
  304. /* buffering reads not possible for prepend mode */
  305. if (!(flags & SPI_XFER_END)) {
  306. debug("unable to buffer reads\n");
  307. return HSSPI_XFER_MODE_DUMMYCS;
  308. }
  309. /* check rx size */
  310. if (data_bytes > HSSPI_MAX_DATA_SIZE) {
  311. debug("max rx bytes exceeded\n");
  312. return HSSPI_XFER_MODE_DUMMYCS;
  313. }
  314. }
  315. if (dout) {
  316. /* check tx size */
  317. if (flags & SPI_XFER_END) {
  318. if (priv->prepend_cnt + data_bytes > HSSPI_MAX_DATA_SIZE) {
  319. debug("max tx bytes exceeded\n");
  320. return HSSPI_XFER_MODE_DUMMYCS;
  321. }
  322. } else {
  323. if (priv->prepend_cnt + data_bytes > HSSPI_MAX_PREPEND_SIZE) {
  324. debug("max prepend bytes exceeded\n");
  325. return HSSPI_XFER_MODE_DUMMYCS;
  326. }
  327. /*
  328. * buffer transfer data in the prepend buf in case we have to fall
  329. * back to dummy cs mode.
  330. */
  331. memcpy(&priv->prepend_buf[priv->prepend_cnt], dout, data_bytes);
  332. priv->prepend_cnt += data_bytes;
  333. }
  334. }
  335. return HSSPI_XFER_MODE_PREPEND;
  336. }
  337. static int bcm63xx_hsspi_xfer_prepend(struct udevice *dev, unsigned int data_bytes,
  338. const void *dout, void *din, unsigned long flags)
  339. {
  340. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev->parent);
  341. struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
  342. uint16_t opcode = 0;
  343. uint32_t val, offset;
  344. int ret;
  345. if (flags & SPI_XFER_END) {
  346. offset = HSSPI_FIFO_BASE + HSSPI_FIFO_OP_SIZE;
  347. if (priv->prepend_cnt) {
  348. /* copy prepend data */
  349. memcpy_toio(priv->regs + offset,
  350. priv->prepend_buf, priv->prepend_cnt);
  351. }
  352. if (dout && data_bytes) {
  353. /* copy tx data */
  354. offset += priv->prepend_cnt;
  355. memcpy_toio(priv->regs + offset, dout, data_bytes);
  356. }
  357. bcm63xx_hsspi_activate_cs(priv, plat);
  358. if (dout && !din) {
  359. /* all half-duplex write. merge to single write */
  360. data_bytes += priv->prepend_cnt;
  361. opcode = HSSPI_FIFO_OP_CODE_W;
  362. priv->prepend_cnt = 0;
  363. } else if (!dout && din) {
  364. /* half-duplex read with prepend write */
  365. opcode = HSSPI_FIFO_OP_CODE_R;
  366. } else {
  367. /* full duplex read/write */
  368. opcode = HSSPI_FIFO_OP_READ_WRITE;
  369. }
  370. /* profile mode */
  371. val = SPI_PFL_MODE_FILL_MASK;
  372. if (plat->mode & SPI_3WIRE)
  373. val |= SPI_PFL_MODE_3WIRE_MASK;
  374. /* dual mode */
  375. if ((opcode == HSSPI_FIFO_OP_CODE_R && (plat->mode & SPI_RX_DUAL)) ||
  376. (opcode == HSSPI_FIFO_OP_CODE_W && (plat->mode & SPI_TX_DUAL))) {
  377. opcode |= HSSPI_FIFO_OP_MBIT_MASK;
  378. if (plat->mode & SPI_RX_DUAL) {
  379. val |= SPI_PFL_MODE_MDRDSZ_MASK;
  380. val |= priv->prepend_cnt << SPI_PFL_MODE_MDRDST_SHIFT;
  381. }
  382. if (plat->mode & SPI_TX_DUAL) {
  383. val |= SPI_PFL_MODE_MDWRSZ_MASK;
  384. val |= priv->prepend_cnt << SPI_PFL_MODE_MDWRST_SHIFT;
  385. }
  386. }
  387. val |= (priv->prepend_cnt << SPI_PFL_MODE_PREPCNT_SHIFT);
  388. writel(val, priv->regs + SPI_PFL_MODE_REG(plat->cs));
  389. /* set fifo operation */
  390. val = opcode | (data_bytes & HSSPI_FIFO_OP_BYTES_MASK);
  391. writew(cpu_to_be16(val),
  392. priv->regs + HSSPI_FIFO_OP_REG);
  393. /* issue the transfer */
  394. val = SPI_CMD_OP_START;
  395. val |= (plat->cs << SPI_CMD_PFL_SHIFT) &
  396. SPI_CMD_PFL_MASK;
  397. val |= (plat->cs << SPI_CMD_SLAVE_SHIFT) &
  398. SPI_CMD_SLAVE_MASK;
  399. writel(val, priv->regs + SPI_CMD_REG);
  400. /* wait for completion */
  401. ret = wait_for_bit_32(priv->regs + SPI_STAT_REG,
  402. SPI_STAT_SRCBUSY_MASK, false,
  403. 1000, false);
  404. if (ret) {
  405. bcm63xx_hsspi_deactivate_cs(priv);
  406. printf("spi polling timeout\n");
  407. return ret;
  408. }
  409. /* copy rx data */
  410. if (din)
  411. memcpy_fromio(din, priv->regs + HSSPI_FIFO_BASE,
  412. data_bytes);
  413. bcm63xx_hsspi_deactivate_cs(priv);
  414. }
  415. return 0;
  416. }
  417. static int bcm63xx_hsspi_xfer(struct udevice *dev, unsigned int bitlen,
  418. const void *dout, void *din, unsigned long flags)
  419. {
  420. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev->parent);
  421. int ret;
  422. u32 data_bytes = bitlen >> 3;
  423. if (priv->xfer_mode == HSSPI_XFER_MODE_PREPEND) {
  424. priv->xfer_mode =
  425. bcm63xx_prepare_prepend_transfer(priv, data_bytes, dout, din, flags);
  426. }
  427. /* if not prependable, fall back to dummy cs mode with safe clock */
  428. if (priv->xfer_mode == HSSPI_XFER_MODE_DUMMYCS) {
  429. /* For pending prepend data from previous transfers, send it first */
  430. if (priv->prepend_cnt) {
  431. bcm63xx_hsspi_xfer_dummy_cs(dev, priv->prepend_cnt,
  432. priv->prepend_buf, NULL,
  433. (flags & ~SPI_XFER_END) | SPI_XFER_BEGIN);
  434. priv->prepend_cnt = 0;
  435. }
  436. ret = bcm63xx_hsspi_xfer_dummy_cs(dev, data_bytes, dout, din, flags);
  437. } else {
  438. ret = bcm63xx_hsspi_xfer_prepend(dev, data_bytes, dout, din, flags);
  439. }
  440. if (flags & SPI_XFER_END)
  441. priv->xfer_mode = HSSPI_XFER_MODE_PREPEND;
  442. return ret;
  443. }
  444. static const struct dm_spi_ops bcm63xx_hsspi_ops = {
  445. .cs_info = bcm63xx_hsspi_cs_info,
  446. .set_mode = bcm63xx_hsspi_set_mode,
  447. .set_speed = bcm63xx_hsspi_set_speed,
  448. .xfer = bcm63xx_hsspi_xfer,
  449. };
  450. static const struct udevice_id bcm63xx_hsspi_ids[] = {
  451. { .compatible = "brcm,bcm6328-hsspi", },
  452. { .compatible = "brcm,bcmbca-hsspi-v1.0", },
  453. { /* sentinel */ }
  454. };
  455. static int bcm63xx_hsspi_child_pre_probe(struct udevice *dev)
  456. {
  457. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev->parent);
  458. struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
  459. struct spi_slave *slave = dev_get_parent_priv(dev);
  460. /* check cs */
  461. if (plat->cs >= priv->num_cs) {
  462. printf("no cs %u\n", plat->cs);
  463. return -ENODEV;
  464. }
  465. /* cs polarity */
  466. if (plat->mode & SPI_CS_HIGH)
  467. priv->cs_pols |= BIT(plat->cs);
  468. else
  469. priv->cs_pols &= ~BIT(plat->cs);
  470. /*
  471. * set the max read/write size to make sure each xfer are within the
  472. * prepend limit
  473. */
  474. slave->max_read_size = HSSPI_MAX_DATA_SIZE;
  475. slave->max_write_size = HSSPI_MAX_DATA_SIZE;
  476. return 0;
  477. }
  478. static int bcm63xx_hsspi_probe(struct udevice *dev)
  479. {
  480. struct bcm63xx_hsspi_priv *priv = dev_get_priv(dev);
  481. struct reset_ctl rst_ctl;
  482. struct clk clk;
  483. int ret;
  484. priv->regs = dev_remap_addr(dev);
  485. if (!priv->regs)
  486. return -EINVAL;
  487. priv->num_cs = dev_read_u32_default(dev, "num-cs", 8);
  488. /* enable clock */
  489. ret = clk_get_by_name(dev, "hsspi", &clk);
  490. if (ret < 0)
  491. return ret;
  492. ret = clk_enable(&clk);
  493. if (ret < 0 && ret != -ENOSYS)
  494. return ret;
  495. clk_free(&clk);
  496. /* get clock rate */
  497. ret = clk_get_by_name(dev, "pll", &clk);
  498. if (ret < 0 && ret != -ENOSYS)
  499. return ret;
  500. priv->clk_rate = clk_get_rate(&clk);
  501. clk_free(&clk);
  502. /* perform reset */
  503. ret = reset_get_by_index(dev, 0, &rst_ctl);
  504. if (ret >= 0) {
  505. ret = reset_deassert(&rst_ctl);
  506. if (ret < 0)
  507. return ret;
  508. }
  509. ret = reset_free(&rst_ctl);
  510. if (ret < 0)
  511. return ret;
  512. /* initialize hardware */
  513. writel(0, priv->regs + SPI_IR_MASK_REG);
  514. /* clear pending interrupts */
  515. writel(SPI_IR_CLEAR_ALL, priv->regs + SPI_IR_STAT_REG);
  516. /* enable clk gate */
  517. setbits_32(priv->regs + SPI_CTL_REG, SPI_CTL_CLK_GATE_MASK);
  518. /* read default cs polarities */
  519. priv->cs_pols = readl(priv->regs + SPI_CTL_REG) &
  520. SPI_CTL_CS_POL_MASK;
  521. /* default in prepend mode */
  522. priv->xfer_mode = HSSPI_XFER_MODE_PREPEND;
  523. return 0;
  524. }
  525. U_BOOT_DRIVER(bcm63xx_hsspi) = {
  526. .name = "bcm63xx_hsspi",
  527. .id = UCLASS_SPI,
  528. .of_match = bcm63xx_hsspi_ids,
  529. .ops = &bcm63xx_hsspi_ops,
  530. .priv_auto = sizeof(struct bcm63xx_hsspi_priv),
  531. .child_pre_probe = bcm63xx_hsspi_child_pre_probe,
  532. .probe = bcm63xx_hsspi_probe,
  533. };