spi-davinci.c 29 KB

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