spi-davinci.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * Copyright (C) 2009 Texas Instruments.
  3. * Copyright (C) 2010 EF Johnson Technologies
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/gpio.h>
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/err.h>
  26. #include <linux/clk.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/edma.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/spi/spi.h>
  34. #include <linux/spi/spi_bitbang.h>
  35. #include <linux/slab.h>
  36. #include <linux/platform_data/spi-davinci.h>
  37. #define SPI_NO_RESOURCE ((resource_size_t)-1)
  38. #define CS_DEFAULT 0xFF
  39. #define SPIFMT_PHASE_MASK BIT(16)
  40. #define SPIFMT_POLARITY_MASK BIT(17)
  41. #define SPIFMT_DISTIMER_MASK BIT(18)
  42. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  43. #define SPIFMT_WAITENA_MASK BIT(21)
  44. #define SPIFMT_PARITYENA_MASK BIT(22)
  45. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  46. #define SPIFMT_WDELAY_MASK 0x3f000000u
  47. #define SPIFMT_WDELAY_SHIFT 24
  48. #define SPIFMT_PRESCALE_SHIFT 8
  49. /* SPIPC0 */
  50. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  51. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  52. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  53. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  54. #define SPIINT_MASKALL 0x0101035F
  55. #define SPIINT_MASKINT 0x0000015F
  56. #define SPI_INTLVL_1 0x000001FF
  57. #define SPI_INTLVL_0 0x00000000
  58. /* SPIDAT1 (upper 16 bit defines) */
  59. #define SPIDAT1_CSHOLD_MASK BIT(12)
  60. #define SPIDAT1_WDEL BIT(10)
  61. /* SPIGCR1 */
  62. #define SPIGCR1_CLKMOD_MASK BIT(1)
  63. #define SPIGCR1_MASTER_MASK BIT(0)
  64. #define SPIGCR1_POWERDOWN_MASK BIT(8)
  65. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  66. #define SPIGCR1_SPIENA_MASK BIT(24)
  67. /* SPIBUF */
  68. #define SPIBUF_TXFULL_MASK BIT(29)
  69. #define SPIBUF_RXEMPTY_MASK BIT(31)
  70. /* SPIDELAY */
  71. #define SPIDELAY_C2TDELAY_SHIFT 24
  72. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  73. #define SPIDELAY_T2CDELAY_SHIFT 16
  74. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  75. #define SPIDELAY_T2EDELAY_SHIFT 8
  76. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  77. #define SPIDELAY_C2EDELAY_SHIFT 0
  78. #define SPIDELAY_C2EDELAY_MASK 0xFF
  79. /* Error Masks */
  80. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  81. #define SPIFLG_TIMEOUT_MASK BIT(1)
  82. #define SPIFLG_PARERR_MASK BIT(2)
  83. #define SPIFLG_DESYNC_MASK BIT(3)
  84. #define SPIFLG_BITERR_MASK BIT(4)
  85. #define SPIFLG_OVRRUN_MASK BIT(6)
  86. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  87. #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \
  88. | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \
  89. | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \
  90. | SPIFLG_OVRRUN_MASK)
  91. #define SPIINT_DMA_REQ_EN BIT(16)
  92. /* SPI Controller registers */
  93. #define SPIGCR0 0x00
  94. #define SPIGCR1 0x04
  95. #define SPIINT 0x08
  96. #define SPILVL 0x0c
  97. #define SPIFLG 0x10
  98. #define SPIPC0 0x14
  99. #define SPIDAT1 0x3c
  100. #define SPIBUF 0x40
  101. #define SPIDELAY 0x48
  102. #define SPIDEF 0x4c
  103. #define SPIFMT0 0x50
  104. /* SPI Controller driver's private data. */
  105. struct davinci_spi {
  106. struct spi_bitbang bitbang;
  107. struct clk *clk;
  108. u8 version;
  109. resource_size_t pbase;
  110. void __iomem *base;
  111. u32 irq;
  112. struct completion done;
  113. const void *tx;
  114. void *rx;
  115. int rcount;
  116. int wcount;
  117. struct dma_chan *dma_rx;
  118. struct dma_chan *dma_tx;
  119. int dma_rx_chnum;
  120. int dma_tx_chnum;
  121. struct davinci_spi_platform_data pdata;
  122. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  123. u32 (*get_tx)(struct davinci_spi *);
  124. u8 *bytes_per_word;
  125. };
  126. static struct davinci_spi_config davinci_spi_default_cfg;
  127. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *dspi)
  128. {
  129. if (dspi->rx) {
  130. u8 *rx = dspi->rx;
  131. *rx++ = (u8)data;
  132. dspi->rx = rx;
  133. }
  134. }
  135. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *dspi)
  136. {
  137. if (dspi->rx) {
  138. u16 *rx = dspi->rx;
  139. *rx++ = (u16)data;
  140. dspi->rx = rx;
  141. }
  142. }
  143. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *dspi)
  144. {
  145. u32 data = 0;
  146. if (dspi->tx) {
  147. const u8 *tx = dspi->tx;
  148. data = *tx++;
  149. dspi->tx = tx;
  150. }
  151. return data;
  152. }
  153. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *dspi)
  154. {
  155. u32 data = 0;
  156. if (dspi->tx) {
  157. const u16 *tx = dspi->tx;
  158. data = *tx++;
  159. dspi->tx = tx;
  160. }
  161. return data;
  162. }
  163. static inline void set_io_bits(void __iomem *addr, u32 bits)
  164. {
  165. u32 v = ioread32(addr);
  166. v |= bits;
  167. iowrite32(v, addr);
  168. }
  169. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  170. {
  171. u32 v = ioread32(addr);
  172. v &= ~bits;
  173. iowrite32(v, addr);
  174. }
  175. /*
  176. * Interface to control the chip select signal
  177. */
  178. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  179. {
  180. struct davinci_spi *dspi;
  181. struct davinci_spi_platform_data *pdata;
  182. struct davinci_spi_config *spicfg = spi->controller_data;
  183. u8 chip_sel = spi->chip_select;
  184. u16 spidat1 = CS_DEFAULT;
  185. bool gpio_chipsel = false;
  186. int gpio;
  187. dspi = spi_master_get_devdata(spi->master);
  188. pdata = &dspi->pdata;
  189. if (spi->cs_gpio >= 0) {
  190. /* SPI core parse and update master->cs_gpio */
  191. gpio_chipsel = true;
  192. gpio = spi->cs_gpio;
  193. }
  194. /* program delay transfers if tx_delay is non zero */
  195. if (spicfg->wdelay)
  196. spidat1 |= SPIDAT1_WDEL;
  197. /*
  198. * Board specific chip select logic decides the polarity and cs
  199. * line for the controller
  200. */
  201. if (gpio_chipsel) {
  202. if (value == BITBANG_CS_ACTIVE)
  203. gpio_set_value(gpio, spi->mode & SPI_CS_HIGH);
  204. else
  205. gpio_set_value(gpio, !(spi->mode & SPI_CS_HIGH));
  206. } else {
  207. if (value == BITBANG_CS_ACTIVE) {
  208. spidat1 |= SPIDAT1_CSHOLD_MASK;
  209. spidat1 &= ~(0x1 << chip_sel);
  210. }
  211. }
  212. iowrite16(spidat1, dspi->base + SPIDAT1 + 2);
  213. }
  214. /**
  215. * davinci_spi_get_prescale - Calculates the correct prescale value
  216. * @maxspeed_hz: the maximum rate the SPI clock can run at
  217. *
  218. * This function calculates the prescale value that generates a clock rate
  219. * less than or equal to the specified maximum.
  220. *
  221. * Returns: calculated prescale - 1 for easy programming into SPI registers
  222. * or negative error number if valid prescalar cannot be updated.
  223. */
  224. static inline int davinci_spi_get_prescale(struct davinci_spi *dspi,
  225. u32 max_speed_hz)
  226. {
  227. int ret;
  228. ret = DIV_ROUND_UP(clk_get_rate(dspi->clk), max_speed_hz);
  229. if (ret < 3 || ret > 256)
  230. return -EINVAL;
  231. return ret - 1;
  232. }
  233. /**
  234. * davinci_spi_setup_transfer - This functions will determine transfer method
  235. * @spi: spi device on which data transfer to be done
  236. * @t: spi transfer in which transfer info is filled
  237. *
  238. * This function determines data transfer method (8/16/32 bit transfer).
  239. * It will also set the SPI Clock Control register according to
  240. * SPI slave device freq.
  241. */
  242. static int davinci_spi_setup_transfer(struct spi_device *spi,
  243. struct spi_transfer *t)
  244. {
  245. struct davinci_spi *dspi;
  246. struct davinci_spi_config *spicfg;
  247. u8 bits_per_word = 0;
  248. u32 hz = 0, spifmt = 0;
  249. int prescale;
  250. dspi = spi_master_get_devdata(spi->master);
  251. spicfg = spi->controller_data;
  252. if (!spicfg)
  253. spicfg = &davinci_spi_default_cfg;
  254. if (t) {
  255. bits_per_word = t->bits_per_word;
  256. hz = t->speed_hz;
  257. }
  258. /* if bits_per_word is not set then set it default */
  259. if (!bits_per_word)
  260. bits_per_word = spi->bits_per_word;
  261. /*
  262. * Assign function pointer to appropriate transfer method
  263. * 8bit, 16bit or 32bit transfer
  264. */
  265. if (bits_per_word <= 8) {
  266. dspi->get_rx = davinci_spi_rx_buf_u8;
  267. dspi->get_tx = davinci_spi_tx_buf_u8;
  268. dspi->bytes_per_word[spi->chip_select] = 1;
  269. } else {
  270. dspi->get_rx = davinci_spi_rx_buf_u16;
  271. dspi->get_tx = davinci_spi_tx_buf_u16;
  272. dspi->bytes_per_word[spi->chip_select] = 2;
  273. }
  274. if (!hz)
  275. hz = spi->max_speed_hz;
  276. /* Set up SPIFMTn register, unique to this chipselect. */
  277. prescale = davinci_spi_get_prescale(dspi, hz);
  278. if (prescale < 0)
  279. return prescale;
  280. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  281. if (spi->mode & SPI_LSB_FIRST)
  282. spifmt |= SPIFMT_SHIFTDIR_MASK;
  283. if (spi->mode & SPI_CPOL)
  284. spifmt |= SPIFMT_POLARITY_MASK;
  285. if (!(spi->mode & SPI_CPHA))
  286. spifmt |= SPIFMT_PHASE_MASK;
  287. /*
  288. * Assume wdelay is used only on SPI peripherals that has this field
  289. * in SPIFMTn register and when it's configured from board file or DT.
  290. */
  291. if (spicfg->wdelay)
  292. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  293. & SPIFMT_WDELAY_MASK);
  294. /*
  295. * Version 1 hardware supports two basic SPI modes:
  296. * - Standard SPI mode uses 4 pins, with chipselect
  297. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  298. * (distinct from SPI_3WIRE, with just one data wire;
  299. * or similar variants without MOSI or without MISO)
  300. *
  301. * Version 2 hardware supports an optional handshaking signal,
  302. * so it can support two more modes:
  303. * - 5 pin SPI variant is standard SPI plus SPI_READY
  304. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  305. */
  306. if (dspi->version == SPI_VERSION_2) {
  307. u32 delay = 0;
  308. if (spicfg->odd_parity)
  309. spifmt |= SPIFMT_ODD_PARITY_MASK;
  310. if (spicfg->parity_enable)
  311. spifmt |= SPIFMT_PARITYENA_MASK;
  312. if (spicfg->timer_disable) {
  313. spifmt |= SPIFMT_DISTIMER_MASK;
  314. } else {
  315. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  316. & SPIDELAY_C2TDELAY_MASK;
  317. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  318. & SPIDELAY_T2CDELAY_MASK;
  319. }
  320. if (spi->mode & SPI_READY) {
  321. spifmt |= SPIFMT_WAITENA_MASK;
  322. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  323. & SPIDELAY_T2EDELAY_MASK;
  324. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  325. & SPIDELAY_C2EDELAY_MASK;
  326. }
  327. iowrite32(delay, dspi->base + SPIDELAY);
  328. }
  329. iowrite32(spifmt, dspi->base + SPIFMT0);
  330. return 0;
  331. }
  332. static int davinci_spi_of_setup(struct spi_device *spi)
  333. {
  334. struct davinci_spi_config *spicfg = spi->controller_data;
  335. struct device_node *np = spi->dev.of_node;
  336. u32 prop;
  337. if (spicfg == NULL && np) {
  338. spicfg = kzalloc(sizeof(*spicfg), GFP_KERNEL);
  339. if (!spicfg)
  340. return -ENOMEM;
  341. *spicfg = davinci_spi_default_cfg;
  342. /* override with dt configured values */
  343. if (!of_property_read_u32(np, "ti,spi-wdelay", &prop))
  344. spicfg->wdelay = (u8)prop;
  345. spi->controller_data = spicfg;
  346. }
  347. return 0;
  348. }
  349. /**
  350. * davinci_spi_setup - This functions will set default transfer method
  351. * @spi: spi device on which data transfer to be done
  352. *
  353. * This functions sets the default transfer method.
  354. */
  355. static int davinci_spi_setup(struct spi_device *spi)
  356. {
  357. int retval = 0;
  358. struct davinci_spi *dspi;
  359. struct davinci_spi_platform_data *pdata;
  360. struct spi_master *master = spi->master;
  361. struct device_node *np = spi->dev.of_node;
  362. bool internal_cs = true;
  363. dspi = spi_master_get_devdata(spi->master);
  364. pdata = &dspi->pdata;
  365. if (!(spi->mode & SPI_NO_CS)) {
  366. if (np && (master->cs_gpios != NULL) && (spi->cs_gpio >= 0)) {
  367. retval = gpio_direction_output(
  368. spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  369. internal_cs = false;
  370. } else if (pdata->chip_sel &&
  371. spi->chip_select < pdata->num_chipselect &&
  372. pdata->chip_sel[spi->chip_select] != SPI_INTERN_CS) {
  373. spi->cs_gpio = pdata->chip_sel[spi->chip_select];
  374. retval = gpio_direction_output(
  375. spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  376. internal_cs = false;
  377. }
  378. if (retval) {
  379. dev_err(&spi->dev, "GPIO %d setup failed (%d)\n",
  380. spi->cs_gpio, retval);
  381. return retval;
  382. }
  383. if (internal_cs)
  384. set_io_bits(dspi->base + SPIPC0, 1 << spi->chip_select);
  385. }
  386. if (spi->mode & SPI_READY)
  387. set_io_bits(dspi->base + SPIPC0, SPIPC0_SPIENA_MASK);
  388. if (spi->mode & SPI_LOOP)
  389. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  390. else
  391. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_LOOPBACK_MASK);
  392. return davinci_spi_of_setup(spi);
  393. }
  394. static void davinci_spi_cleanup(struct spi_device *spi)
  395. {
  396. struct davinci_spi_config *spicfg = spi->controller_data;
  397. spi->controller_data = NULL;
  398. if (spi->dev.of_node)
  399. kfree(spicfg);
  400. }
  401. static int davinci_spi_check_error(struct davinci_spi *dspi, int int_status)
  402. {
  403. struct device *sdev = dspi->bitbang.master->dev.parent;
  404. if (int_status & SPIFLG_TIMEOUT_MASK) {
  405. dev_dbg(sdev, "SPI Time-out Error\n");
  406. return -ETIMEDOUT;
  407. }
  408. if (int_status & SPIFLG_DESYNC_MASK) {
  409. dev_dbg(sdev, "SPI Desynchronization Error\n");
  410. return -EIO;
  411. }
  412. if (int_status & SPIFLG_BITERR_MASK) {
  413. dev_dbg(sdev, "SPI Bit error\n");
  414. return -EIO;
  415. }
  416. if (dspi->version == SPI_VERSION_2) {
  417. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  418. dev_dbg(sdev, "SPI Data Length Error\n");
  419. return -EIO;
  420. }
  421. if (int_status & SPIFLG_PARERR_MASK) {
  422. dev_dbg(sdev, "SPI Parity Error\n");
  423. return -EIO;
  424. }
  425. if (int_status & SPIFLG_OVRRUN_MASK) {
  426. dev_dbg(sdev, "SPI Data Overrun error\n");
  427. return -EIO;
  428. }
  429. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  430. dev_dbg(sdev, "SPI Buffer Init Active\n");
  431. return -EBUSY;
  432. }
  433. }
  434. return 0;
  435. }
  436. /**
  437. * davinci_spi_process_events - check for and handle any SPI controller events
  438. * @dspi: the controller data
  439. *
  440. * This function will check the SPIFLG register and handle any events that are
  441. * detected there
  442. */
  443. static int davinci_spi_process_events(struct davinci_spi *dspi)
  444. {
  445. u32 buf, status, errors = 0, spidat1;
  446. buf = ioread32(dspi->base + SPIBUF);
  447. if (dspi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) {
  448. dspi->get_rx(buf & 0xFFFF, dspi);
  449. dspi->rcount--;
  450. }
  451. status = ioread32(dspi->base + SPIFLG);
  452. if (unlikely(status & SPIFLG_ERROR_MASK)) {
  453. errors = status & SPIFLG_ERROR_MASK;
  454. goto out;
  455. }
  456. if (dspi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) {
  457. spidat1 = ioread32(dspi->base + SPIDAT1);
  458. dspi->wcount--;
  459. spidat1 &= ~0xFFFF;
  460. spidat1 |= 0xFFFF & dspi->get_tx(dspi);
  461. iowrite32(spidat1, dspi->base + SPIDAT1);
  462. }
  463. out:
  464. return errors;
  465. }
  466. static void davinci_spi_dma_rx_callback(void *data)
  467. {
  468. struct davinci_spi *dspi = (struct davinci_spi *)data;
  469. dspi->rcount = 0;
  470. if (!dspi->wcount && !dspi->rcount)
  471. complete(&dspi->done);
  472. }
  473. static void davinci_spi_dma_tx_callback(void *data)
  474. {
  475. struct davinci_spi *dspi = (struct davinci_spi *)data;
  476. dspi->wcount = 0;
  477. if (!dspi->wcount && !dspi->rcount)
  478. complete(&dspi->done);
  479. }
  480. /**
  481. * davinci_spi_bufs - functions which will handle transfer data
  482. * @spi: spi device on which data transfer to be done
  483. * @t: spi transfer in which transfer info is filled
  484. *
  485. * This function will put data to be transferred into data register
  486. * of SPI controller and then wait until the completion will be marked
  487. * by the IRQ Handler.
  488. */
  489. static int davinci_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
  490. {
  491. struct davinci_spi *dspi;
  492. int data_type, ret = -ENOMEM;
  493. u32 tx_data, spidat1;
  494. u32 errors = 0;
  495. struct davinci_spi_config *spicfg;
  496. struct davinci_spi_platform_data *pdata;
  497. unsigned uninitialized_var(rx_buf_count);
  498. void *dummy_buf = NULL;
  499. struct scatterlist sg_rx, sg_tx;
  500. dspi = spi_master_get_devdata(spi->master);
  501. pdata = &dspi->pdata;
  502. spicfg = (struct davinci_spi_config *)spi->controller_data;
  503. if (!spicfg)
  504. spicfg = &davinci_spi_default_cfg;
  505. /* convert len to words based on bits_per_word */
  506. data_type = dspi->bytes_per_word[spi->chip_select];
  507. dspi->tx = t->tx_buf;
  508. dspi->rx = t->rx_buf;
  509. dspi->wcount = t->len / data_type;
  510. dspi->rcount = dspi->wcount;
  511. spidat1 = ioread32(dspi->base + SPIDAT1);
  512. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  513. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  514. reinit_completion(&dspi->done);
  515. if (spicfg->io_type == SPI_IO_TYPE_INTR)
  516. set_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  517. if (spicfg->io_type != SPI_IO_TYPE_DMA) {
  518. /* start the transfer */
  519. dspi->wcount--;
  520. tx_data = dspi->get_tx(dspi);
  521. spidat1 &= 0xFFFF0000;
  522. spidat1 |= tx_data & 0xFFFF;
  523. iowrite32(spidat1, dspi->base + SPIDAT1);
  524. } else {
  525. struct dma_slave_config dma_rx_conf = {
  526. .direction = DMA_DEV_TO_MEM,
  527. .src_addr = (unsigned long)dspi->pbase + SPIBUF,
  528. .src_addr_width = data_type,
  529. .src_maxburst = 1,
  530. };
  531. struct dma_slave_config dma_tx_conf = {
  532. .direction = DMA_MEM_TO_DEV,
  533. .dst_addr = (unsigned long)dspi->pbase + SPIDAT1,
  534. .dst_addr_width = data_type,
  535. .dst_maxburst = 1,
  536. };
  537. struct dma_async_tx_descriptor *rxdesc;
  538. struct dma_async_tx_descriptor *txdesc;
  539. void *buf;
  540. dummy_buf = kzalloc(t->len, GFP_KERNEL);
  541. if (!dummy_buf)
  542. goto err_alloc_dummy_buf;
  543. dmaengine_slave_config(dspi->dma_rx, &dma_rx_conf);
  544. dmaengine_slave_config(dspi->dma_tx, &dma_tx_conf);
  545. sg_init_table(&sg_rx, 1);
  546. if (!t->rx_buf)
  547. buf = dummy_buf;
  548. else
  549. buf = t->rx_buf;
  550. t->rx_dma = dma_map_single(&spi->dev, buf,
  551. t->len, DMA_FROM_DEVICE);
  552. if (!t->rx_dma) {
  553. ret = -EFAULT;
  554. goto err_rx_map;
  555. }
  556. sg_dma_address(&sg_rx) = t->rx_dma;
  557. sg_dma_len(&sg_rx) = t->len;
  558. sg_init_table(&sg_tx, 1);
  559. if (!t->tx_buf)
  560. buf = dummy_buf;
  561. else
  562. buf = (void *)t->tx_buf;
  563. t->tx_dma = dma_map_single(&spi->dev, buf,
  564. t->len, DMA_TO_DEVICE);
  565. if (!t->tx_dma) {
  566. ret = -EFAULT;
  567. goto err_tx_map;
  568. }
  569. sg_dma_address(&sg_tx) = t->tx_dma;
  570. sg_dma_len(&sg_tx) = t->len;
  571. rxdesc = dmaengine_prep_slave_sg(dspi->dma_rx,
  572. &sg_rx, 1, DMA_DEV_TO_MEM,
  573. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  574. if (!rxdesc)
  575. goto err_desc;
  576. txdesc = dmaengine_prep_slave_sg(dspi->dma_tx,
  577. &sg_tx, 1, DMA_MEM_TO_DEV,
  578. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  579. if (!txdesc)
  580. goto err_desc;
  581. rxdesc->callback = davinci_spi_dma_rx_callback;
  582. rxdesc->callback_param = (void *)dspi;
  583. txdesc->callback = davinci_spi_dma_tx_callback;
  584. txdesc->callback_param = (void *)dspi;
  585. if (pdata->cshold_bug)
  586. iowrite16(spidat1 >> 16, dspi->base + SPIDAT1 + 2);
  587. dmaengine_submit(rxdesc);
  588. dmaengine_submit(txdesc);
  589. dma_async_issue_pending(dspi->dma_rx);
  590. dma_async_issue_pending(dspi->dma_tx);
  591. set_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  592. }
  593. /* Wait for the transfer to complete */
  594. if (spicfg->io_type != SPI_IO_TYPE_POLL) {
  595. wait_for_completion_interruptible(&(dspi->done));
  596. } else {
  597. while (dspi->rcount > 0 || dspi->wcount > 0) {
  598. errors = davinci_spi_process_events(dspi);
  599. if (errors)
  600. break;
  601. cpu_relax();
  602. }
  603. }
  604. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKALL);
  605. if (spicfg->io_type == SPI_IO_TYPE_DMA) {
  606. clear_io_bits(dspi->base + SPIINT, SPIINT_DMA_REQ_EN);
  607. dma_unmap_single(&spi->dev, t->rx_dma,
  608. t->len, DMA_FROM_DEVICE);
  609. dma_unmap_single(&spi->dev, t->tx_dma,
  610. t->len, DMA_TO_DEVICE);
  611. kfree(dummy_buf);
  612. }
  613. clear_io_bits(dspi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  614. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  615. /*
  616. * Check for bit error, desync error,parity error,timeout error and
  617. * receive overflow errors
  618. */
  619. if (errors) {
  620. ret = davinci_spi_check_error(dspi, errors);
  621. WARN(!ret, "%s: error reported but no error found!\n",
  622. dev_name(&spi->dev));
  623. return ret;
  624. }
  625. if (dspi->rcount != 0 || dspi->wcount != 0) {
  626. dev_err(&spi->dev, "SPI data transfer error\n");
  627. return -EIO;
  628. }
  629. return t->len;
  630. err_desc:
  631. dma_unmap_single(&spi->dev, t->tx_dma, t->len, DMA_TO_DEVICE);
  632. err_tx_map:
  633. dma_unmap_single(&spi->dev, t->rx_dma, t->len, DMA_FROM_DEVICE);
  634. err_rx_map:
  635. kfree(dummy_buf);
  636. err_alloc_dummy_buf:
  637. return ret;
  638. }
  639. /**
  640. * dummy_thread_fn - dummy thread function
  641. * @irq: IRQ number for this SPI Master
  642. * @context_data: structure for SPI Master controller davinci_spi
  643. *
  644. * This is to satisfy the request_threaded_irq() API so that the irq
  645. * handler is called in interrupt context.
  646. */
  647. static irqreturn_t dummy_thread_fn(s32 irq, void *data)
  648. {
  649. return IRQ_HANDLED;
  650. }
  651. /**
  652. * davinci_spi_irq - Interrupt handler for SPI Master Controller
  653. * @irq: IRQ number for this SPI Master
  654. * @context_data: structure for SPI Master controller davinci_spi
  655. *
  656. * ISR will determine that interrupt arrives either for READ or WRITE command.
  657. * According to command it will do the appropriate action. It will check
  658. * transfer length and if it is not zero then dispatch transfer command again.
  659. * If transfer length is zero then it will indicate the COMPLETION so that
  660. * davinci_spi_bufs function can go ahead.
  661. */
  662. static irqreturn_t davinci_spi_irq(s32 irq, void *data)
  663. {
  664. struct davinci_spi *dspi = data;
  665. int status;
  666. status = davinci_spi_process_events(dspi);
  667. if (unlikely(status != 0))
  668. clear_io_bits(dspi->base + SPIINT, SPIINT_MASKINT);
  669. if ((!dspi->rcount && !dspi->wcount) || status)
  670. complete(&dspi->done);
  671. return IRQ_HANDLED;
  672. }
  673. static int davinci_spi_request_dma(struct davinci_spi *dspi)
  674. {
  675. dma_cap_mask_t mask;
  676. struct device *sdev = dspi->bitbang.master->dev.parent;
  677. int r;
  678. dma_cap_zero(mask);
  679. dma_cap_set(DMA_SLAVE, mask);
  680. dspi->dma_rx = dma_request_channel(mask, edma_filter_fn,
  681. &dspi->dma_rx_chnum);
  682. if (!dspi->dma_rx) {
  683. dev_err(sdev, "request RX DMA channel failed\n");
  684. r = -ENODEV;
  685. goto rx_dma_failed;
  686. }
  687. dspi->dma_tx = dma_request_channel(mask, edma_filter_fn,
  688. &dspi->dma_tx_chnum);
  689. if (!dspi->dma_tx) {
  690. dev_err(sdev, "request TX DMA channel failed\n");
  691. r = -ENODEV;
  692. goto tx_dma_failed;
  693. }
  694. return 0;
  695. tx_dma_failed:
  696. dma_release_channel(dspi->dma_rx);
  697. rx_dma_failed:
  698. return r;
  699. }
  700. #if defined(CONFIG_OF)
  701. static const struct of_device_id davinci_spi_of_match[] = {
  702. {
  703. .compatible = "ti,dm6441-spi",
  704. },
  705. {
  706. .compatible = "ti,da830-spi",
  707. .data = (void *)SPI_VERSION_2,
  708. },
  709. { },
  710. };
  711. MODULE_DEVICE_TABLE(of, davinci_spi_of_match);
  712. /**
  713. * spi_davinci_get_pdata - Get platform data from DTS binding
  714. * @pdev: ptr to platform data
  715. * @dspi: ptr to driver data
  716. *
  717. * Parses and populates pdata in dspi from device tree bindings.
  718. *
  719. * NOTE: Not all platform data params are supported currently.
  720. */
  721. static int spi_davinci_get_pdata(struct platform_device *pdev,
  722. struct davinci_spi *dspi)
  723. {
  724. struct device_node *node = pdev->dev.of_node;
  725. struct davinci_spi_platform_data *pdata;
  726. unsigned int num_cs, intr_line = 0;
  727. const struct of_device_id *match;
  728. pdata = &dspi->pdata;
  729. pdata->version = SPI_VERSION_1;
  730. match = of_match_device(davinci_spi_of_match, &pdev->dev);
  731. if (!match)
  732. return -ENODEV;
  733. /* match data has the SPI version number for SPI_VERSION_2 */
  734. if (match->data == (void *)SPI_VERSION_2)
  735. pdata->version = SPI_VERSION_2;
  736. /*
  737. * default num_cs is 1 and all chipsel are internal to the chip
  738. * indicated by chip_sel being NULL or cs_gpios being NULL or
  739. * set to -ENOENT. num-cs includes internal as well as gpios.
  740. * indicated by chip_sel being NULL. GPIO based CS is not
  741. * supported yet in DT bindings.
  742. */
  743. num_cs = 1;
  744. of_property_read_u32(node, "num-cs", &num_cs);
  745. pdata->num_chipselect = num_cs;
  746. of_property_read_u32(node, "ti,davinci-spi-intr-line", &intr_line);
  747. pdata->intr_line = intr_line;
  748. return 0;
  749. }
  750. #else
  751. static struct davinci_spi_platform_data
  752. *spi_davinci_get_pdata(struct platform_device *pdev,
  753. struct davinci_spi *dspi)
  754. {
  755. return -ENODEV;
  756. }
  757. #endif
  758. /**
  759. * davinci_spi_probe - probe function for SPI Master Controller
  760. * @pdev: platform_device structure which contains plateform specific data
  761. *
  762. * According to Linux Device Model this function will be invoked by Linux
  763. * with platform_device struct which contains the device specific info.
  764. * This function will map the SPI controller's memory, register IRQ,
  765. * Reset SPI controller and setting its registers to default value.
  766. * It will invoke spi_bitbang_start to create work queue so that client driver
  767. * can register transfer method to work queue.
  768. */
  769. static int davinci_spi_probe(struct platform_device *pdev)
  770. {
  771. struct spi_master *master;
  772. struct davinci_spi *dspi;
  773. struct davinci_spi_platform_data *pdata;
  774. struct resource *r;
  775. resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
  776. resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
  777. int ret = 0;
  778. u32 spipc0;
  779. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  780. if (master == NULL) {
  781. ret = -ENOMEM;
  782. goto err;
  783. }
  784. platform_set_drvdata(pdev, master);
  785. dspi = spi_master_get_devdata(master);
  786. if (dev_get_platdata(&pdev->dev)) {
  787. pdata = dev_get_platdata(&pdev->dev);
  788. dspi->pdata = *pdata;
  789. } else {
  790. /* update dspi pdata with that from the DT */
  791. ret = spi_davinci_get_pdata(pdev, dspi);
  792. if (ret < 0)
  793. goto free_master;
  794. }
  795. /* pdata in dspi is now updated and point pdata to that */
  796. pdata = &dspi->pdata;
  797. dspi->bytes_per_word = devm_kzalloc(&pdev->dev,
  798. sizeof(*dspi->bytes_per_word) *
  799. pdata->num_chipselect, GFP_KERNEL);
  800. if (dspi->bytes_per_word == NULL) {
  801. ret = -ENOMEM;
  802. goto free_master;
  803. }
  804. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  805. if (r == NULL) {
  806. ret = -ENOENT;
  807. goto free_master;
  808. }
  809. dspi->pbase = r->start;
  810. dspi->base = devm_ioremap_resource(&pdev->dev, r);
  811. if (IS_ERR(dspi->base)) {
  812. ret = PTR_ERR(dspi->base);
  813. goto free_master;
  814. }
  815. dspi->irq = platform_get_irq(pdev, 0);
  816. if (dspi->irq <= 0) {
  817. ret = -EINVAL;
  818. goto free_master;
  819. }
  820. ret = devm_request_threaded_irq(&pdev->dev, dspi->irq, davinci_spi_irq,
  821. dummy_thread_fn, 0, dev_name(&pdev->dev), dspi);
  822. if (ret)
  823. goto free_master;
  824. dspi->bitbang.master = master;
  825. dspi->clk = devm_clk_get(&pdev->dev, NULL);
  826. if (IS_ERR(dspi->clk)) {
  827. ret = -ENODEV;
  828. goto free_master;
  829. }
  830. clk_prepare_enable(dspi->clk);
  831. master->dev.of_node = pdev->dev.of_node;
  832. master->bus_num = pdev->id;
  833. master->num_chipselect = pdata->num_chipselect;
  834. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 16);
  835. master->setup = davinci_spi_setup;
  836. master->cleanup = davinci_spi_cleanup;
  837. dspi->bitbang.chipselect = davinci_spi_chipselect;
  838. dspi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  839. dspi->version = pdata->version;
  840. dspi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  841. if (dspi->version == SPI_VERSION_2)
  842. dspi->bitbang.flags |= SPI_READY;
  843. if (pdev->dev.of_node) {
  844. int i;
  845. for (i = 0; i < pdata->num_chipselect; i++) {
  846. int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
  847. "cs-gpios", i);
  848. if (cs_gpio == -EPROBE_DEFER) {
  849. ret = cs_gpio;
  850. goto free_clk;
  851. }
  852. if (gpio_is_valid(cs_gpio)) {
  853. ret = devm_gpio_request(&pdev->dev, cs_gpio,
  854. dev_name(&pdev->dev));
  855. if (ret)
  856. goto free_clk;
  857. }
  858. }
  859. }
  860. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  861. if (r)
  862. dma_rx_chan = r->start;
  863. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  864. if (r)
  865. dma_tx_chan = r->start;
  866. dspi->bitbang.txrx_bufs = davinci_spi_bufs;
  867. if (dma_rx_chan != SPI_NO_RESOURCE &&
  868. dma_tx_chan != SPI_NO_RESOURCE) {
  869. dspi->dma_rx_chnum = dma_rx_chan;
  870. dspi->dma_tx_chnum = dma_tx_chan;
  871. ret = davinci_spi_request_dma(dspi);
  872. if (ret)
  873. goto free_clk;
  874. dev_info(&pdev->dev, "DMA: supported\n");
  875. dev_info(&pdev->dev, "DMA: RX channel: %pa, TX channel: %pa, event queue: %d\n",
  876. &dma_rx_chan, &dma_tx_chan,
  877. pdata->dma_event_q);
  878. }
  879. dspi->get_rx = davinci_spi_rx_buf_u8;
  880. dspi->get_tx = davinci_spi_tx_buf_u8;
  881. init_completion(&dspi->done);
  882. /* Reset In/OUT SPI module */
  883. iowrite32(0, dspi->base + SPIGCR0);
  884. udelay(100);
  885. iowrite32(1, dspi->base + SPIGCR0);
  886. /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */
  887. spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK;
  888. iowrite32(spipc0, dspi->base + SPIPC0);
  889. if (pdata->intr_line)
  890. iowrite32(SPI_INTLVL_1, dspi->base + SPILVL);
  891. else
  892. iowrite32(SPI_INTLVL_0, dspi->base + SPILVL);
  893. iowrite32(CS_DEFAULT, dspi->base + SPIDEF);
  894. /* master mode default */
  895. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_CLKMOD_MASK);
  896. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  897. set_io_bits(dspi->base + SPIGCR1, SPIGCR1_POWERDOWN_MASK);
  898. ret = spi_bitbang_start(&dspi->bitbang);
  899. if (ret)
  900. goto free_dma;
  901. dev_info(&pdev->dev, "Controller at 0x%p\n", dspi->base);
  902. return ret;
  903. free_dma:
  904. dma_release_channel(dspi->dma_rx);
  905. dma_release_channel(dspi->dma_tx);
  906. free_clk:
  907. clk_disable_unprepare(dspi->clk);
  908. free_master:
  909. spi_master_put(master);
  910. err:
  911. return ret;
  912. }
  913. /**
  914. * davinci_spi_remove - remove function for SPI Master Controller
  915. * @pdev: platform_device structure which contains plateform specific data
  916. *
  917. * This function will do the reverse action of davinci_spi_probe function
  918. * It will free the IRQ and SPI controller's memory region.
  919. * It will also call spi_bitbang_stop to destroy the work queue which was
  920. * created by spi_bitbang_start.
  921. */
  922. static int davinci_spi_remove(struct platform_device *pdev)
  923. {
  924. struct davinci_spi *dspi;
  925. struct spi_master *master;
  926. master = platform_get_drvdata(pdev);
  927. dspi = spi_master_get_devdata(master);
  928. spi_bitbang_stop(&dspi->bitbang);
  929. clk_disable_unprepare(dspi->clk);
  930. spi_master_put(master);
  931. return 0;
  932. }
  933. static struct platform_driver davinci_spi_driver = {
  934. .driver = {
  935. .name = "spi_davinci",
  936. .owner = THIS_MODULE,
  937. .of_match_table = of_match_ptr(davinci_spi_of_match),
  938. },
  939. .probe = davinci_spi_probe,
  940. .remove = davinci_spi_remove,
  941. };
  942. module_platform_driver(davinci_spi_driver);
  943. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  944. MODULE_LICENSE("GPL");