spi-bcm63xx.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * Broadcom BCM63xx SPI controller support
  3. *
  4. * Copyright (C) 2009-2012 Florian Fainelli <florian@openwrt.org>
  5. * Copyright (C) 2010 Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/completion.h>
  26. #include <linux/err.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/of.h>
  29. /* BCM 6338/6348 SPI core */
  30. #define SPI_6348_RSET_SIZE 64
  31. #define SPI_6348_CMD 0x00 /* 16-bits register */
  32. #define SPI_6348_INT_STATUS 0x02
  33. #define SPI_6348_INT_MASK_ST 0x03
  34. #define SPI_6348_INT_MASK 0x04
  35. #define SPI_6348_ST 0x05
  36. #define SPI_6348_CLK_CFG 0x06
  37. #define SPI_6348_FILL_BYTE 0x07
  38. #define SPI_6348_MSG_TAIL 0x09
  39. #define SPI_6348_RX_TAIL 0x0b
  40. #define SPI_6348_MSG_CTL 0x40 /* 8-bits register */
  41. #define SPI_6348_MSG_CTL_WIDTH 8
  42. #define SPI_6348_MSG_DATA 0x41
  43. #define SPI_6348_MSG_DATA_SIZE 0x3f
  44. #define SPI_6348_RX_DATA 0x80
  45. #define SPI_6348_RX_DATA_SIZE 0x3f
  46. /* BCM 3368/6358/6262/6368 SPI core */
  47. #define SPI_6358_RSET_SIZE 1804
  48. #define SPI_6358_MSG_CTL 0x00 /* 16-bits register */
  49. #define SPI_6358_MSG_CTL_WIDTH 16
  50. #define SPI_6358_MSG_DATA 0x02
  51. #define SPI_6358_MSG_DATA_SIZE 0x21e
  52. #define SPI_6358_RX_DATA 0x400
  53. #define SPI_6358_RX_DATA_SIZE 0x220
  54. #define SPI_6358_CMD 0x700 /* 16-bits register */
  55. #define SPI_6358_INT_STATUS 0x702
  56. #define SPI_6358_INT_MASK_ST 0x703
  57. #define SPI_6358_INT_MASK 0x704
  58. #define SPI_6358_ST 0x705
  59. #define SPI_6358_CLK_CFG 0x706
  60. #define SPI_6358_FILL_BYTE 0x707
  61. #define SPI_6358_MSG_TAIL 0x709
  62. #define SPI_6358_RX_TAIL 0x70B
  63. /* Shared SPI definitions */
  64. /* Message configuration */
  65. #define SPI_FD_RW 0x00
  66. #define SPI_HD_W 0x01
  67. #define SPI_HD_R 0x02
  68. #define SPI_BYTE_CNT_SHIFT 0
  69. #define SPI_6348_MSG_TYPE_SHIFT 6
  70. #define SPI_6358_MSG_TYPE_SHIFT 14
  71. /* Command */
  72. #define SPI_CMD_NOOP 0x00
  73. #define SPI_CMD_SOFT_RESET 0x01
  74. #define SPI_CMD_HARD_RESET 0x02
  75. #define SPI_CMD_START_IMMEDIATE 0x03
  76. #define SPI_CMD_COMMAND_SHIFT 0
  77. #define SPI_CMD_COMMAND_MASK 0x000f
  78. #define SPI_CMD_DEVICE_ID_SHIFT 4
  79. #define SPI_CMD_PREPEND_BYTE_CNT_SHIFT 8
  80. #define SPI_CMD_ONE_BYTE_SHIFT 11
  81. #define SPI_CMD_ONE_WIRE_SHIFT 12
  82. #define SPI_DEV_ID_0 0
  83. #define SPI_DEV_ID_1 1
  84. #define SPI_DEV_ID_2 2
  85. #define SPI_DEV_ID_3 3
  86. /* Interrupt mask */
  87. #define SPI_INTR_CMD_DONE 0x01
  88. #define SPI_INTR_RX_OVERFLOW 0x02
  89. #define SPI_INTR_TX_UNDERFLOW 0x04
  90. #define SPI_INTR_TX_OVERFLOW 0x08
  91. #define SPI_INTR_RX_UNDERFLOW 0x10
  92. #define SPI_INTR_CLEAR_ALL 0x1f
  93. /* Status */
  94. #define SPI_RX_EMPTY 0x02
  95. #define SPI_CMD_BUSY 0x04
  96. #define SPI_SERIAL_BUSY 0x08
  97. /* Clock configuration */
  98. #define SPI_CLK_20MHZ 0x00
  99. #define SPI_CLK_0_391MHZ 0x01
  100. #define SPI_CLK_0_781MHZ 0x02 /* default */
  101. #define SPI_CLK_1_563MHZ 0x03
  102. #define SPI_CLK_3_125MHZ 0x04
  103. #define SPI_CLK_6_250MHZ 0x05
  104. #define SPI_CLK_12_50MHZ 0x06
  105. #define SPI_CLK_MASK 0x07
  106. #define SPI_SSOFFTIME_MASK 0x38
  107. #define SPI_SSOFFTIME_SHIFT 3
  108. #define SPI_BYTE_SWAP 0x80
  109. enum bcm63xx_regs_spi {
  110. SPI_CMD,
  111. SPI_INT_STATUS,
  112. SPI_INT_MASK_ST,
  113. SPI_INT_MASK,
  114. SPI_ST,
  115. SPI_CLK_CFG,
  116. SPI_FILL_BYTE,
  117. SPI_MSG_TAIL,
  118. SPI_RX_TAIL,
  119. SPI_MSG_CTL,
  120. SPI_MSG_DATA,
  121. SPI_RX_DATA,
  122. SPI_MSG_TYPE_SHIFT,
  123. SPI_MSG_CTL_WIDTH,
  124. SPI_MSG_DATA_SIZE,
  125. };
  126. #define BCM63XX_SPI_MAX_PREPEND 15
  127. #define BCM63XX_SPI_MAX_CS 8
  128. #define BCM63XX_SPI_BUS_NUM 0
  129. struct bcm63xx_spi {
  130. struct completion done;
  131. void __iomem *regs;
  132. int irq;
  133. /* Platform data */
  134. const unsigned long *reg_offsets;
  135. unsigned int fifo_size;
  136. unsigned int msg_type_shift;
  137. unsigned int msg_ctl_width;
  138. /* data iomem */
  139. u8 __iomem *tx_io;
  140. const u8 __iomem *rx_io;
  141. struct clk *clk;
  142. struct platform_device *pdev;
  143. };
  144. static inline u8 bcm_spi_readb(struct bcm63xx_spi *bs,
  145. unsigned int offset)
  146. {
  147. return readb(bs->regs + bs->reg_offsets[offset]);
  148. }
  149. static inline u16 bcm_spi_readw(struct bcm63xx_spi *bs,
  150. unsigned int offset)
  151. {
  152. #ifdef CONFIG_CPU_BIG_ENDIAN
  153. return ioread16be(bs->regs + bs->reg_offsets[offset]);
  154. #else
  155. return readw(bs->regs + bs->reg_offsets[offset]);
  156. #endif
  157. }
  158. static inline void bcm_spi_writeb(struct bcm63xx_spi *bs,
  159. u8 value, unsigned int offset)
  160. {
  161. writeb(value, bs->regs + bs->reg_offsets[offset]);
  162. }
  163. static inline void bcm_spi_writew(struct bcm63xx_spi *bs,
  164. u16 value, unsigned int offset)
  165. {
  166. #ifdef CONFIG_CPU_BIG_ENDIAN
  167. iowrite16be(value, bs->regs + bs->reg_offsets[offset]);
  168. #else
  169. writew(value, bs->regs + bs->reg_offsets[offset]);
  170. #endif
  171. }
  172. static const unsigned int bcm63xx_spi_freq_table[SPI_CLK_MASK][2] = {
  173. { 20000000, SPI_CLK_20MHZ },
  174. { 12500000, SPI_CLK_12_50MHZ },
  175. { 6250000, SPI_CLK_6_250MHZ },
  176. { 3125000, SPI_CLK_3_125MHZ },
  177. { 1563000, SPI_CLK_1_563MHZ },
  178. { 781000, SPI_CLK_0_781MHZ },
  179. { 391000, SPI_CLK_0_391MHZ }
  180. };
  181. static void bcm63xx_spi_setup_transfer(struct spi_device *spi,
  182. struct spi_transfer *t)
  183. {
  184. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  185. u8 clk_cfg, reg;
  186. int i;
  187. /* Default to lowest clock configuration */
  188. clk_cfg = SPI_CLK_0_391MHZ;
  189. /* Find the closest clock configuration */
  190. for (i = 0; i < SPI_CLK_MASK; i++) {
  191. if (t->speed_hz >= bcm63xx_spi_freq_table[i][0]) {
  192. clk_cfg = bcm63xx_spi_freq_table[i][1];
  193. break;
  194. }
  195. }
  196. /* clear existing clock configuration bits of the register */
  197. reg = bcm_spi_readb(bs, SPI_CLK_CFG);
  198. reg &= ~SPI_CLK_MASK;
  199. reg |= clk_cfg;
  200. bcm_spi_writeb(bs, reg, SPI_CLK_CFG);
  201. dev_dbg(&spi->dev, "Setting clock register to %02x (hz %d)\n",
  202. clk_cfg, t->speed_hz);
  203. }
  204. /* the spi->mode bits understood by this driver: */
  205. #define MODEBITS (SPI_CPOL | SPI_CPHA)
  206. static int bcm63xx_txrx_bufs(struct spi_device *spi, struct spi_transfer *first,
  207. unsigned int num_transfers)
  208. {
  209. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  210. u16 msg_ctl;
  211. u16 cmd;
  212. unsigned int i, timeout = 0, prepend_len = 0, len = 0;
  213. struct spi_transfer *t = first;
  214. bool do_rx = false;
  215. bool do_tx = false;
  216. /* Disable the CMD_DONE interrupt */
  217. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  218. dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n",
  219. t->tx_buf, t->rx_buf, t->len);
  220. if (num_transfers > 1 && t->tx_buf && t->len <= BCM63XX_SPI_MAX_PREPEND)
  221. prepend_len = t->len;
  222. /* prepare the buffer */
  223. for (i = 0; i < num_transfers; i++) {
  224. if (t->tx_buf) {
  225. do_tx = true;
  226. memcpy_toio(bs->tx_io + len, t->tx_buf, t->len);
  227. /* don't prepend more than one tx */
  228. if (t != first)
  229. prepend_len = 0;
  230. }
  231. if (t->rx_buf) {
  232. do_rx = true;
  233. /* prepend is half-duplex write only */
  234. if (t == first)
  235. prepend_len = 0;
  236. }
  237. len += t->len;
  238. t = list_entry(t->transfer_list.next, struct spi_transfer,
  239. transfer_list);
  240. }
  241. reinit_completion(&bs->done);
  242. /* Fill in the Message control register */
  243. msg_ctl = (len << SPI_BYTE_CNT_SHIFT);
  244. if (do_rx && do_tx && prepend_len == 0)
  245. msg_ctl |= (SPI_FD_RW << bs->msg_type_shift);
  246. else if (do_rx)
  247. msg_ctl |= (SPI_HD_R << bs->msg_type_shift);
  248. else if (do_tx)
  249. msg_ctl |= (SPI_HD_W << bs->msg_type_shift);
  250. switch (bs->msg_ctl_width) {
  251. case 8:
  252. bcm_spi_writeb(bs, msg_ctl, SPI_MSG_CTL);
  253. break;
  254. case 16:
  255. bcm_spi_writew(bs, msg_ctl, SPI_MSG_CTL);
  256. break;
  257. }
  258. /* Issue the transfer */
  259. cmd = SPI_CMD_START_IMMEDIATE;
  260. cmd |= (prepend_len << SPI_CMD_PREPEND_BYTE_CNT_SHIFT);
  261. cmd |= (spi->chip_select << SPI_CMD_DEVICE_ID_SHIFT);
  262. bcm_spi_writew(bs, cmd, SPI_CMD);
  263. /* Enable the CMD_DONE interrupt */
  264. bcm_spi_writeb(bs, SPI_INTR_CMD_DONE, SPI_INT_MASK);
  265. timeout = wait_for_completion_timeout(&bs->done, HZ);
  266. if (!timeout)
  267. return -ETIMEDOUT;
  268. if (!do_rx)
  269. return 0;
  270. len = 0;
  271. t = first;
  272. /* Read out all the data */
  273. for (i = 0; i < num_transfers; i++) {
  274. if (t->rx_buf)
  275. memcpy_fromio(t->rx_buf, bs->rx_io + len, t->len);
  276. if (t != first || prepend_len == 0)
  277. len += t->len;
  278. t = list_entry(t->transfer_list.next, struct spi_transfer,
  279. transfer_list);
  280. }
  281. return 0;
  282. }
  283. static int bcm63xx_spi_transfer_one(struct spi_master *master,
  284. struct spi_message *m)
  285. {
  286. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  287. struct spi_transfer *t, *first = NULL;
  288. struct spi_device *spi = m->spi;
  289. int status = 0;
  290. unsigned int n_transfers = 0, total_len = 0;
  291. bool can_use_prepend = false;
  292. /*
  293. * This SPI controller does not support keeping CS active after a
  294. * transfer.
  295. * Work around this by merging as many transfers we can into one big
  296. * full-duplex transfers.
  297. */
  298. list_for_each_entry(t, &m->transfers, transfer_list) {
  299. if (!first)
  300. first = t;
  301. n_transfers++;
  302. total_len += t->len;
  303. if (n_transfers == 2 && !first->rx_buf && !t->tx_buf &&
  304. first->len <= BCM63XX_SPI_MAX_PREPEND)
  305. can_use_prepend = true;
  306. else if (can_use_prepend && t->tx_buf)
  307. can_use_prepend = false;
  308. /* we can only transfer one fifo worth of data */
  309. if ((can_use_prepend &&
  310. total_len > (bs->fifo_size + BCM63XX_SPI_MAX_PREPEND)) ||
  311. (!can_use_prepend && total_len > bs->fifo_size)) {
  312. dev_err(&spi->dev, "unable to do transfers larger than FIFO size (%i > %i)\n",
  313. total_len, bs->fifo_size);
  314. status = -EINVAL;
  315. goto exit;
  316. }
  317. /* all combined transfers have to have the same speed */
  318. if (t->speed_hz != first->speed_hz) {
  319. dev_err(&spi->dev, "unable to change speed between transfers\n");
  320. status = -EINVAL;
  321. goto exit;
  322. }
  323. /* CS will be deasserted directly after transfer */
  324. if (t->delay_usecs) {
  325. dev_err(&spi->dev, "unable to keep CS asserted after transfer\n");
  326. status = -EINVAL;
  327. goto exit;
  328. }
  329. if (t->cs_change ||
  330. list_is_last(&t->transfer_list, &m->transfers)) {
  331. /* configure adapter for a new transfer */
  332. bcm63xx_spi_setup_transfer(spi, first);
  333. /* send the data */
  334. status = bcm63xx_txrx_bufs(spi, first, n_transfers);
  335. if (status)
  336. goto exit;
  337. m->actual_length += total_len;
  338. first = NULL;
  339. n_transfers = 0;
  340. total_len = 0;
  341. can_use_prepend = false;
  342. }
  343. }
  344. exit:
  345. m->status = status;
  346. spi_finalize_current_message(master);
  347. return 0;
  348. }
  349. /* This driver supports single master mode only. Hence
  350. * CMD_DONE is the only interrupt we care about
  351. */
  352. static irqreturn_t bcm63xx_spi_interrupt(int irq, void *dev_id)
  353. {
  354. struct spi_master *master = (struct spi_master *)dev_id;
  355. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  356. u8 intr;
  357. /* Read interupts and clear them immediately */
  358. intr = bcm_spi_readb(bs, SPI_INT_STATUS);
  359. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  360. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  361. /* A transfer completed */
  362. if (intr & SPI_INTR_CMD_DONE)
  363. complete(&bs->done);
  364. return IRQ_HANDLED;
  365. }
  366. static size_t bcm63xx_spi_max_length(struct spi_device *spi)
  367. {
  368. struct bcm63xx_spi *bs = spi_master_get_devdata(spi->master);
  369. return bs->fifo_size;
  370. }
  371. static const unsigned long bcm6348_spi_reg_offsets[] = {
  372. [SPI_CMD] = SPI_6348_CMD,
  373. [SPI_INT_STATUS] = SPI_6348_INT_STATUS,
  374. [SPI_INT_MASK_ST] = SPI_6348_INT_MASK_ST,
  375. [SPI_INT_MASK] = SPI_6348_INT_MASK,
  376. [SPI_ST] = SPI_6348_ST,
  377. [SPI_CLK_CFG] = SPI_6348_CLK_CFG,
  378. [SPI_FILL_BYTE] = SPI_6348_FILL_BYTE,
  379. [SPI_MSG_TAIL] = SPI_6348_MSG_TAIL,
  380. [SPI_RX_TAIL] = SPI_6348_RX_TAIL,
  381. [SPI_MSG_CTL] = SPI_6348_MSG_CTL,
  382. [SPI_MSG_DATA] = SPI_6348_MSG_DATA,
  383. [SPI_RX_DATA] = SPI_6348_RX_DATA,
  384. [SPI_MSG_TYPE_SHIFT] = SPI_6348_MSG_TYPE_SHIFT,
  385. [SPI_MSG_CTL_WIDTH] = SPI_6348_MSG_CTL_WIDTH,
  386. [SPI_MSG_DATA_SIZE] = SPI_6348_MSG_DATA_SIZE,
  387. };
  388. static const unsigned long bcm6358_spi_reg_offsets[] = {
  389. [SPI_CMD] = SPI_6358_CMD,
  390. [SPI_INT_STATUS] = SPI_6358_INT_STATUS,
  391. [SPI_INT_MASK_ST] = SPI_6358_INT_MASK_ST,
  392. [SPI_INT_MASK] = SPI_6358_INT_MASK,
  393. [SPI_ST] = SPI_6358_ST,
  394. [SPI_CLK_CFG] = SPI_6358_CLK_CFG,
  395. [SPI_FILL_BYTE] = SPI_6358_FILL_BYTE,
  396. [SPI_MSG_TAIL] = SPI_6358_MSG_TAIL,
  397. [SPI_RX_TAIL] = SPI_6358_RX_TAIL,
  398. [SPI_MSG_CTL] = SPI_6358_MSG_CTL,
  399. [SPI_MSG_DATA] = SPI_6358_MSG_DATA,
  400. [SPI_RX_DATA] = SPI_6358_RX_DATA,
  401. [SPI_MSG_TYPE_SHIFT] = SPI_6358_MSG_TYPE_SHIFT,
  402. [SPI_MSG_CTL_WIDTH] = SPI_6358_MSG_CTL_WIDTH,
  403. [SPI_MSG_DATA_SIZE] = SPI_6358_MSG_DATA_SIZE,
  404. };
  405. static const struct platform_device_id bcm63xx_spi_dev_match[] = {
  406. {
  407. .name = "bcm6348-spi",
  408. .driver_data = (unsigned long)bcm6348_spi_reg_offsets,
  409. },
  410. {
  411. .name = "bcm6358-spi",
  412. .driver_data = (unsigned long)bcm6358_spi_reg_offsets,
  413. },
  414. {
  415. },
  416. };
  417. static const struct of_device_id bcm63xx_spi_of_match[] = {
  418. { .compatible = "brcm,bcm6348-spi", .data = &bcm6348_spi_reg_offsets },
  419. { .compatible = "brcm,bcm6358-spi", .data = &bcm6358_spi_reg_offsets },
  420. { },
  421. };
  422. static int bcm63xx_spi_probe(struct platform_device *pdev)
  423. {
  424. struct resource *r;
  425. const unsigned long *bcm63xx_spireg;
  426. struct device *dev = &pdev->dev;
  427. int irq, bus_num;
  428. struct spi_master *master;
  429. struct clk *clk;
  430. struct bcm63xx_spi *bs;
  431. int ret;
  432. u32 num_cs = BCM63XX_SPI_MAX_CS;
  433. if (dev->of_node) {
  434. const struct of_device_id *match;
  435. match = of_match_node(bcm63xx_spi_of_match, dev->of_node);
  436. if (!match)
  437. return -EINVAL;
  438. bcm63xx_spireg = match->data;
  439. of_property_read_u32(dev->of_node, "num-cs", &num_cs);
  440. if (num_cs > BCM63XX_SPI_MAX_CS) {
  441. dev_warn(dev, "unsupported number of cs (%i), reducing to 8\n",
  442. num_cs);
  443. num_cs = BCM63XX_SPI_MAX_CS;
  444. }
  445. bus_num = -1;
  446. } else if (pdev->id_entry->driver_data) {
  447. const struct platform_device_id *match = pdev->id_entry;
  448. bcm63xx_spireg = (const unsigned long *)match->driver_data;
  449. bus_num = BCM63XX_SPI_BUS_NUM;
  450. } else {
  451. return -EINVAL;
  452. }
  453. irq = platform_get_irq(pdev, 0);
  454. if (irq < 0) {
  455. dev_err(dev, "no irq: %d\n", irq);
  456. return irq;
  457. }
  458. clk = devm_clk_get(dev, "spi");
  459. if (IS_ERR(clk)) {
  460. dev_err(dev, "no clock for device\n");
  461. return PTR_ERR(clk);
  462. }
  463. master = spi_alloc_master(dev, sizeof(*bs));
  464. if (!master) {
  465. dev_err(dev, "out of memory\n");
  466. return -ENOMEM;
  467. }
  468. bs = spi_master_get_devdata(master);
  469. init_completion(&bs->done);
  470. platform_set_drvdata(pdev, master);
  471. bs->pdev = pdev;
  472. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  473. bs->regs = devm_ioremap_resource(&pdev->dev, r);
  474. if (IS_ERR(bs->regs)) {
  475. ret = PTR_ERR(bs->regs);
  476. goto out_err;
  477. }
  478. bs->irq = irq;
  479. bs->clk = clk;
  480. bs->reg_offsets = bcm63xx_spireg;
  481. bs->fifo_size = bs->reg_offsets[SPI_MSG_DATA_SIZE];
  482. ret = devm_request_irq(&pdev->dev, irq, bcm63xx_spi_interrupt, 0,
  483. pdev->name, master);
  484. if (ret) {
  485. dev_err(dev, "unable to request irq\n");
  486. goto out_err;
  487. }
  488. master->dev.of_node = dev->of_node;
  489. master->bus_num = bus_num;
  490. master->num_chipselect = num_cs;
  491. master->transfer_one_message = bcm63xx_spi_transfer_one;
  492. master->mode_bits = MODEBITS;
  493. master->bits_per_word_mask = SPI_BPW_MASK(8);
  494. master->max_transfer_size = bcm63xx_spi_max_length;
  495. master->max_message_size = bcm63xx_spi_max_length;
  496. master->auto_runtime_pm = true;
  497. bs->msg_type_shift = bs->reg_offsets[SPI_MSG_TYPE_SHIFT];
  498. bs->msg_ctl_width = bs->reg_offsets[SPI_MSG_CTL_WIDTH];
  499. bs->tx_io = (u8 *)(bs->regs + bs->reg_offsets[SPI_MSG_DATA]);
  500. bs->rx_io = (const u8 *)(bs->regs + bs->reg_offsets[SPI_RX_DATA]);
  501. /* Initialize hardware */
  502. ret = clk_prepare_enable(bs->clk);
  503. if (ret)
  504. goto out_err;
  505. bcm_spi_writeb(bs, SPI_INTR_CLEAR_ALL, SPI_INT_STATUS);
  506. /* register and we are done */
  507. ret = devm_spi_register_master(dev, master);
  508. if (ret) {
  509. dev_err(dev, "spi register failed\n");
  510. goto out_clk_disable;
  511. }
  512. dev_info(dev, "at %pr (irq %d, FIFOs size %d)\n",
  513. r, irq, bs->fifo_size);
  514. return 0;
  515. out_clk_disable:
  516. clk_disable_unprepare(clk);
  517. out_err:
  518. spi_master_put(master);
  519. return ret;
  520. }
  521. static int bcm63xx_spi_remove(struct platform_device *pdev)
  522. {
  523. struct spi_master *master = platform_get_drvdata(pdev);
  524. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  525. /* reset spi block */
  526. bcm_spi_writeb(bs, 0, SPI_INT_MASK);
  527. /* HW shutdown */
  528. clk_disable_unprepare(bs->clk);
  529. return 0;
  530. }
  531. #ifdef CONFIG_PM_SLEEP
  532. static int bcm63xx_spi_suspend(struct device *dev)
  533. {
  534. struct spi_master *master = dev_get_drvdata(dev);
  535. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  536. spi_master_suspend(master);
  537. clk_disable_unprepare(bs->clk);
  538. return 0;
  539. }
  540. static int bcm63xx_spi_resume(struct device *dev)
  541. {
  542. struct spi_master *master = dev_get_drvdata(dev);
  543. struct bcm63xx_spi *bs = spi_master_get_devdata(master);
  544. int ret;
  545. ret = clk_prepare_enable(bs->clk);
  546. if (ret)
  547. return ret;
  548. spi_master_resume(master);
  549. return 0;
  550. }
  551. #endif
  552. static const struct dev_pm_ops bcm63xx_spi_pm_ops = {
  553. SET_SYSTEM_SLEEP_PM_OPS(bcm63xx_spi_suspend, bcm63xx_spi_resume)
  554. };
  555. static struct platform_driver bcm63xx_spi_driver = {
  556. .driver = {
  557. .name = "bcm63xx-spi",
  558. .pm = &bcm63xx_spi_pm_ops,
  559. .of_match_table = bcm63xx_spi_of_match,
  560. },
  561. .id_table = bcm63xx_spi_dev_match,
  562. .probe = bcm63xx_spi_probe,
  563. .remove = bcm63xx_spi_remove,
  564. };
  565. module_platform_driver(bcm63xx_spi_driver);
  566. MODULE_ALIAS("platform:bcm63xx_spi");
  567. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  568. MODULE_AUTHOR("Tanguy Bouzeloc <tanguy.bouzeloc@efixo.com>");
  569. MODULE_DESCRIPTION("Broadcom BCM63xx SPI Controller driver");
  570. MODULE_LICENSE("GPL");