davinci_spi.c 29 KB

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