davinci_spi.c 30 KB

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