spi-bcm63xx.c 16 KB

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