spi-omap2-mcspi.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. * OMAP2 McSPI controller driver
  3. *
  4. * Copyright (C) 2005, 2006 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
  6. * Juha Yrj�l� <juha.yrjola@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/module.h>
  21. #include <linux/device.h>
  22. #include <linux/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/err.h>
  28. #include <linux/clk.h>
  29. #include <linux/io.h>
  30. #include <linux/slab.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/gcd.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/gpio.h>
  37. #include <linux/platform_data/spi-omap2-mcspi.h>
  38. #define OMAP2_MCSPI_MAX_FREQ 48000000
  39. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  40. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  41. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  42. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  43. #define OMAP2_MCSPI_REVISION 0x00
  44. #define OMAP2_MCSPI_SYSSTATUS 0x14
  45. #define OMAP2_MCSPI_IRQSTATUS 0x18
  46. #define OMAP2_MCSPI_IRQENABLE 0x1c
  47. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  48. #define OMAP2_MCSPI_SYST 0x24
  49. #define OMAP2_MCSPI_MODULCTRL 0x28
  50. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  51. /* per-channel banks, 0x14 bytes each, first is: */
  52. #define OMAP2_MCSPI_CHCONF0 0x2c
  53. #define OMAP2_MCSPI_CHSTAT0 0x30
  54. #define OMAP2_MCSPI_CHCTRL0 0x34
  55. #define OMAP2_MCSPI_TX0 0x38
  56. #define OMAP2_MCSPI_RX0 0x3c
  57. /* per-register bitmasks: */
  58. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  59. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  60. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  61. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  62. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  63. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  64. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  65. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  66. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  67. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  68. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  69. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  70. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  71. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  72. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  73. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  74. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  75. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  76. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  77. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  78. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  79. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  80. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  81. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  82. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  83. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  84. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  85. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  86. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  87. /* We have 2 DMA channels per CS, one for RX and one for TX */
  88. struct omap2_mcspi_dma {
  89. struct dma_chan *dma_tx;
  90. struct dma_chan *dma_rx;
  91. struct completion dma_tx_completion;
  92. struct completion dma_rx_completion;
  93. char dma_rx_ch_name[14];
  94. char dma_tx_ch_name[14];
  95. };
  96. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  97. * cache operations; better heuristics consider wordsize and bitrate.
  98. */
  99. #define DMA_MIN_BYTES 160
  100. /*
  101. * Used for context save and restore, structure members to be updated whenever
  102. * corresponding registers are modified.
  103. */
  104. struct omap2_mcspi_regs {
  105. u32 modulctrl;
  106. u32 wakeupenable;
  107. struct list_head cs;
  108. };
  109. struct omap2_mcspi {
  110. struct spi_master *master;
  111. /* Virtual base address of the controller */
  112. void __iomem *base;
  113. unsigned long phys;
  114. /* SPI1 has 4 channels, while SPI2 has 2 */
  115. struct omap2_mcspi_dma *dma_channels;
  116. struct device *dev;
  117. struct omap2_mcspi_regs ctx;
  118. int fifo_depth;
  119. unsigned int pin_dir:1;
  120. };
  121. struct omap2_mcspi_cs {
  122. void __iomem *base;
  123. unsigned long phys;
  124. int word_len;
  125. u16 mode;
  126. struct list_head node;
  127. /* Context save and restore shadow register */
  128. u32 chconf0, chctrl0;
  129. };
  130. static inline void mcspi_write_reg(struct spi_master *master,
  131. int idx, u32 val)
  132. {
  133. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  134. writel_relaxed(val, mcspi->base + idx);
  135. }
  136. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  137. {
  138. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  139. return readl_relaxed(mcspi->base + idx);
  140. }
  141. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  142. int idx, u32 val)
  143. {
  144. struct omap2_mcspi_cs *cs = spi->controller_state;
  145. writel_relaxed(val, cs->base + idx);
  146. }
  147. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  148. {
  149. struct omap2_mcspi_cs *cs = spi->controller_state;
  150. return readl_relaxed(cs->base + idx);
  151. }
  152. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  153. {
  154. struct omap2_mcspi_cs *cs = spi->controller_state;
  155. return cs->chconf0;
  156. }
  157. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  158. {
  159. struct omap2_mcspi_cs *cs = spi->controller_state;
  160. cs->chconf0 = val;
  161. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  162. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  163. }
  164. static inline int mcspi_bytes_per_word(int word_len)
  165. {
  166. if (word_len <= 8)
  167. return 1;
  168. else if (word_len <= 16)
  169. return 2;
  170. else /* word_len <= 32 */
  171. return 4;
  172. }
  173. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  174. int is_read, int enable)
  175. {
  176. u32 l, rw;
  177. l = mcspi_cached_chconf0(spi);
  178. if (is_read) /* 1 is read, 0 write */
  179. rw = OMAP2_MCSPI_CHCONF_DMAR;
  180. else
  181. rw = OMAP2_MCSPI_CHCONF_DMAW;
  182. if (enable)
  183. l |= rw;
  184. else
  185. l &= ~rw;
  186. mcspi_write_chconf0(spi, l);
  187. }
  188. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  189. {
  190. struct omap2_mcspi_cs *cs = spi->controller_state;
  191. u32 l;
  192. l = cs->chctrl0;
  193. if (enable)
  194. l |= OMAP2_MCSPI_CHCTRL_EN;
  195. else
  196. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  197. cs->chctrl0 = l;
  198. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  199. /* Flash post-writes */
  200. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  201. }
  202. static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
  203. {
  204. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  205. u32 l;
  206. /* The controller handles the inverted chip selects
  207. * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
  208. * the inversion from the core spi_set_cs function.
  209. */
  210. if (spi->mode & SPI_CS_HIGH)
  211. enable = !enable;
  212. if (spi->controller_state) {
  213. int err = pm_runtime_get_sync(mcspi->dev);
  214. if (err < 0) {
  215. pm_runtime_put_noidle(mcspi->dev);
  216. dev_err(mcspi->dev, "failed to get sync: %d\n", err);
  217. return;
  218. }
  219. l = mcspi_cached_chconf0(spi);
  220. if (enable)
  221. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  222. else
  223. l |= OMAP2_MCSPI_CHCONF_FORCE;
  224. mcspi_write_chconf0(spi, l);
  225. pm_runtime_mark_last_busy(mcspi->dev);
  226. pm_runtime_put_autosuspend(mcspi->dev);
  227. }
  228. }
  229. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  230. {
  231. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  232. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  233. u32 l;
  234. /*
  235. * Setup when switching from (reset default) slave mode
  236. * to single-channel master mode
  237. */
  238. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  239. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
  240. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  241. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  242. ctx->modulctrl = l;
  243. }
  244. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  245. struct spi_transfer *t, int enable)
  246. {
  247. struct spi_master *master = spi->master;
  248. struct omap2_mcspi_cs *cs = spi->controller_state;
  249. struct omap2_mcspi *mcspi;
  250. unsigned int wcnt;
  251. int max_fifo_depth, bytes_per_word;
  252. u32 chconf, xferlevel;
  253. mcspi = spi_master_get_devdata(master);
  254. chconf = mcspi_cached_chconf0(spi);
  255. if (enable) {
  256. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  257. if (t->len % bytes_per_word != 0)
  258. goto disable_fifo;
  259. if (t->rx_buf != NULL && t->tx_buf != NULL)
  260. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  261. else
  262. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  263. wcnt = t->len / bytes_per_word;
  264. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  265. goto disable_fifo;
  266. xferlevel = wcnt << 16;
  267. if (t->rx_buf != NULL) {
  268. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  269. xferlevel |= (bytes_per_word - 1) << 8;
  270. }
  271. if (t->tx_buf != NULL) {
  272. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  273. xferlevel |= bytes_per_word - 1;
  274. }
  275. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  276. mcspi_write_chconf0(spi, chconf);
  277. mcspi->fifo_depth = max_fifo_depth;
  278. return;
  279. }
  280. disable_fifo:
  281. if (t->rx_buf != NULL)
  282. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  283. if (t->tx_buf != NULL)
  284. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  285. mcspi_write_chconf0(spi, chconf);
  286. mcspi->fifo_depth = 0;
  287. }
  288. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  289. {
  290. unsigned long timeout;
  291. timeout = jiffies + msecs_to_jiffies(1000);
  292. while (!(readl_relaxed(reg) & bit)) {
  293. if (time_after(jiffies, timeout)) {
  294. if (!(readl_relaxed(reg) & bit))
  295. return -ETIMEDOUT;
  296. else
  297. return 0;
  298. }
  299. cpu_relax();
  300. }
  301. return 0;
  302. }
  303. static void omap2_mcspi_rx_callback(void *data)
  304. {
  305. struct spi_device *spi = data;
  306. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  307. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  308. /* We must disable the DMA RX request */
  309. omap2_mcspi_set_dma_req(spi, 1, 0);
  310. complete(&mcspi_dma->dma_rx_completion);
  311. }
  312. static void omap2_mcspi_tx_callback(void *data)
  313. {
  314. struct spi_device *spi = data;
  315. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  316. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  317. /* We must disable the DMA TX request */
  318. omap2_mcspi_set_dma_req(spi, 0, 0);
  319. complete(&mcspi_dma->dma_tx_completion);
  320. }
  321. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  322. struct spi_transfer *xfer,
  323. struct dma_slave_config cfg)
  324. {
  325. struct omap2_mcspi *mcspi;
  326. struct omap2_mcspi_dma *mcspi_dma;
  327. mcspi = spi_master_get_devdata(spi->master);
  328. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  329. if (mcspi_dma->dma_tx) {
  330. struct dma_async_tx_descriptor *tx;
  331. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  332. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
  333. xfer->tx_sg.nents,
  334. DMA_MEM_TO_DEV,
  335. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  336. if (tx) {
  337. tx->callback = omap2_mcspi_tx_callback;
  338. tx->callback_param = spi;
  339. dmaengine_submit(tx);
  340. } else {
  341. /* FIXME: fall back to PIO? */
  342. }
  343. }
  344. dma_async_issue_pending(mcspi_dma->dma_tx);
  345. omap2_mcspi_set_dma_req(spi, 0, 1);
  346. }
  347. static unsigned
  348. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  349. struct dma_slave_config cfg,
  350. unsigned es)
  351. {
  352. struct omap2_mcspi *mcspi;
  353. struct omap2_mcspi_dma *mcspi_dma;
  354. unsigned int count, transfer_reduction = 0;
  355. struct scatterlist *sg_out[2];
  356. int nb_sizes = 0, out_mapped_nents[2], ret, x;
  357. size_t sizes[2];
  358. u32 l;
  359. int elements = 0;
  360. int word_len, element_count;
  361. struct omap2_mcspi_cs *cs = spi->controller_state;
  362. void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  363. mcspi = spi_master_get_devdata(spi->master);
  364. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  365. count = xfer->len;
  366. /*
  367. * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
  368. * it mentions reducing DMA transfer length by one element in master
  369. * normal mode.
  370. */
  371. if (mcspi->fifo_depth == 0)
  372. transfer_reduction = es;
  373. word_len = cs->word_len;
  374. l = mcspi_cached_chconf0(spi);
  375. if (word_len <= 8)
  376. element_count = count;
  377. else if (word_len <= 16)
  378. element_count = count >> 1;
  379. else /* word_len <= 32 */
  380. element_count = count >> 2;
  381. if (mcspi_dma->dma_rx) {
  382. struct dma_async_tx_descriptor *tx;
  383. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  384. /*
  385. * Reduce DMA transfer length by one more if McSPI is
  386. * configured in turbo mode.
  387. */
  388. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  389. transfer_reduction += es;
  390. if (transfer_reduction) {
  391. /* Split sgl into two. The second sgl won't be used. */
  392. sizes[0] = count - transfer_reduction;
  393. sizes[1] = transfer_reduction;
  394. nb_sizes = 2;
  395. } else {
  396. /*
  397. * Don't bother splitting the sgl. This essentially
  398. * clones the original sgl.
  399. */
  400. sizes[0] = count;
  401. nb_sizes = 1;
  402. }
  403. ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents,
  404. 0, nb_sizes,
  405. sizes,
  406. sg_out, out_mapped_nents,
  407. GFP_KERNEL);
  408. if (ret < 0) {
  409. dev_err(&spi->dev, "sg_split failed\n");
  410. return 0;
  411. }
  412. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx,
  413. sg_out[0],
  414. out_mapped_nents[0],
  415. DMA_DEV_TO_MEM,
  416. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  417. if (tx) {
  418. tx->callback = omap2_mcspi_rx_callback;
  419. tx->callback_param = spi;
  420. dmaengine_submit(tx);
  421. } else {
  422. /* FIXME: fall back to PIO? */
  423. }
  424. }
  425. dma_async_issue_pending(mcspi_dma->dma_rx);
  426. omap2_mcspi_set_dma_req(spi, 1, 1);
  427. wait_for_completion(&mcspi_dma->dma_rx_completion);
  428. for (x = 0; x < nb_sizes; x++)
  429. kfree(sg_out[x]);
  430. if (mcspi->fifo_depth > 0)
  431. return count;
  432. /*
  433. * Due to the DMA transfer length reduction the missing bytes must
  434. * be read manually to receive all of the expected data.
  435. */
  436. omap2_mcspi_set_enable(spi, 0);
  437. elements = element_count - 1;
  438. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  439. elements--;
  440. if (!mcspi_wait_for_reg_bit(chstat_reg,
  441. OMAP2_MCSPI_CHSTAT_RXS)) {
  442. u32 w;
  443. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  444. if (word_len <= 8)
  445. ((u8 *)xfer->rx_buf)[elements++] = w;
  446. else if (word_len <= 16)
  447. ((u16 *)xfer->rx_buf)[elements++] = w;
  448. else /* word_len <= 32 */
  449. ((u32 *)xfer->rx_buf)[elements++] = w;
  450. } else {
  451. int bytes_per_word = mcspi_bytes_per_word(word_len);
  452. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  453. count -= (bytes_per_word << 1);
  454. omap2_mcspi_set_enable(spi, 1);
  455. return count;
  456. }
  457. }
  458. if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
  459. u32 w;
  460. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  461. if (word_len <= 8)
  462. ((u8 *)xfer->rx_buf)[elements] = w;
  463. else if (word_len <= 16)
  464. ((u16 *)xfer->rx_buf)[elements] = w;
  465. else /* word_len <= 32 */
  466. ((u32 *)xfer->rx_buf)[elements] = w;
  467. } else {
  468. dev_err(&spi->dev, "DMA RX last word empty\n");
  469. count -= mcspi_bytes_per_word(word_len);
  470. }
  471. omap2_mcspi_set_enable(spi, 1);
  472. return count;
  473. }
  474. static unsigned
  475. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  476. {
  477. struct omap2_mcspi *mcspi;
  478. struct omap2_mcspi_cs *cs = spi->controller_state;
  479. struct omap2_mcspi_dma *mcspi_dma;
  480. unsigned int count;
  481. u8 *rx;
  482. const u8 *tx;
  483. struct dma_slave_config cfg;
  484. enum dma_slave_buswidth width;
  485. unsigned es;
  486. void __iomem *chstat_reg;
  487. void __iomem *irqstat_reg;
  488. int wait_res;
  489. mcspi = spi_master_get_devdata(spi->master);
  490. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  491. if (cs->word_len <= 8) {
  492. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  493. es = 1;
  494. } else if (cs->word_len <= 16) {
  495. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  496. es = 2;
  497. } else {
  498. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  499. es = 4;
  500. }
  501. count = xfer->len;
  502. memset(&cfg, 0, sizeof(cfg));
  503. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  504. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  505. cfg.src_addr_width = width;
  506. cfg.dst_addr_width = width;
  507. cfg.src_maxburst = 1;
  508. cfg.dst_maxburst = 1;
  509. rx = xfer->rx_buf;
  510. tx = xfer->tx_buf;
  511. if (tx != NULL)
  512. omap2_mcspi_tx_dma(spi, xfer, cfg);
  513. if (rx != NULL)
  514. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  515. if (tx != NULL) {
  516. wait_for_completion(&mcspi_dma->dma_tx_completion);
  517. if (mcspi->fifo_depth > 0) {
  518. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  519. if (mcspi_wait_for_reg_bit(irqstat_reg,
  520. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  521. dev_err(&spi->dev, "EOW timed out\n");
  522. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  523. OMAP2_MCSPI_IRQSTATUS_EOW);
  524. }
  525. /* for TX_ONLY mode, be sure all words have shifted out */
  526. if (rx == NULL) {
  527. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  528. if (mcspi->fifo_depth > 0) {
  529. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  530. OMAP2_MCSPI_CHSTAT_TXFFE);
  531. if (wait_res < 0)
  532. dev_err(&spi->dev, "TXFFE timed out\n");
  533. } else {
  534. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  535. OMAP2_MCSPI_CHSTAT_TXS);
  536. if (wait_res < 0)
  537. dev_err(&spi->dev, "TXS timed out\n");
  538. }
  539. if (wait_res >= 0 &&
  540. (mcspi_wait_for_reg_bit(chstat_reg,
  541. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  542. dev_err(&spi->dev, "EOT timed out\n");
  543. }
  544. }
  545. return count;
  546. }
  547. static unsigned
  548. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  549. {
  550. struct omap2_mcspi_cs *cs = spi->controller_state;
  551. unsigned int count, c;
  552. u32 l;
  553. void __iomem *base = cs->base;
  554. void __iomem *tx_reg;
  555. void __iomem *rx_reg;
  556. void __iomem *chstat_reg;
  557. int word_len;
  558. count = xfer->len;
  559. c = count;
  560. word_len = cs->word_len;
  561. l = mcspi_cached_chconf0(spi);
  562. /* We store the pre-calculated register addresses on stack to speed
  563. * up the transfer loop. */
  564. tx_reg = base + OMAP2_MCSPI_TX0;
  565. rx_reg = base + OMAP2_MCSPI_RX0;
  566. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  567. if (c < (word_len>>3))
  568. return 0;
  569. if (word_len <= 8) {
  570. u8 *rx;
  571. const u8 *tx;
  572. rx = xfer->rx_buf;
  573. tx = xfer->tx_buf;
  574. do {
  575. c -= 1;
  576. if (tx != NULL) {
  577. if (mcspi_wait_for_reg_bit(chstat_reg,
  578. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  579. dev_err(&spi->dev, "TXS timed out\n");
  580. goto out;
  581. }
  582. dev_vdbg(&spi->dev, "write-%d %02x\n",
  583. word_len, *tx);
  584. writel_relaxed(*tx++, tx_reg);
  585. }
  586. if (rx != NULL) {
  587. if (mcspi_wait_for_reg_bit(chstat_reg,
  588. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  589. dev_err(&spi->dev, "RXS timed out\n");
  590. goto out;
  591. }
  592. if (c == 1 && tx == NULL &&
  593. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  594. omap2_mcspi_set_enable(spi, 0);
  595. *rx++ = readl_relaxed(rx_reg);
  596. dev_vdbg(&spi->dev, "read-%d %02x\n",
  597. word_len, *(rx - 1));
  598. if (mcspi_wait_for_reg_bit(chstat_reg,
  599. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  600. dev_err(&spi->dev,
  601. "RXS timed out\n");
  602. goto out;
  603. }
  604. c = 0;
  605. } else if (c == 0 && tx == NULL) {
  606. omap2_mcspi_set_enable(spi, 0);
  607. }
  608. *rx++ = readl_relaxed(rx_reg);
  609. dev_vdbg(&spi->dev, "read-%d %02x\n",
  610. word_len, *(rx - 1));
  611. }
  612. } while (c);
  613. } else if (word_len <= 16) {
  614. u16 *rx;
  615. const u16 *tx;
  616. rx = xfer->rx_buf;
  617. tx = xfer->tx_buf;
  618. do {
  619. c -= 2;
  620. if (tx != NULL) {
  621. if (mcspi_wait_for_reg_bit(chstat_reg,
  622. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  623. dev_err(&spi->dev, "TXS timed out\n");
  624. goto out;
  625. }
  626. dev_vdbg(&spi->dev, "write-%d %04x\n",
  627. word_len, *tx);
  628. writel_relaxed(*tx++, tx_reg);
  629. }
  630. if (rx != NULL) {
  631. if (mcspi_wait_for_reg_bit(chstat_reg,
  632. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  633. dev_err(&spi->dev, "RXS timed out\n");
  634. goto out;
  635. }
  636. if (c == 2 && tx == NULL &&
  637. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  638. omap2_mcspi_set_enable(spi, 0);
  639. *rx++ = readl_relaxed(rx_reg);
  640. dev_vdbg(&spi->dev, "read-%d %04x\n",
  641. word_len, *(rx - 1));
  642. if (mcspi_wait_for_reg_bit(chstat_reg,
  643. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  644. dev_err(&spi->dev,
  645. "RXS timed out\n");
  646. goto out;
  647. }
  648. c = 0;
  649. } else if (c == 0 && tx == NULL) {
  650. omap2_mcspi_set_enable(spi, 0);
  651. }
  652. *rx++ = readl_relaxed(rx_reg);
  653. dev_vdbg(&spi->dev, "read-%d %04x\n",
  654. word_len, *(rx - 1));
  655. }
  656. } while (c >= 2);
  657. } else if (word_len <= 32) {
  658. u32 *rx;
  659. const u32 *tx;
  660. rx = xfer->rx_buf;
  661. tx = xfer->tx_buf;
  662. do {
  663. c -= 4;
  664. if (tx != NULL) {
  665. if (mcspi_wait_for_reg_bit(chstat_reg,
  666. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  667. dev_err(&spi->dev, "TXS timed out\n");
  668. goto out;
  669. }
  670. dev_vdbg(&spi->dev, "write-%d %08x\n",
  671. word_len, *tx);
  672. writel_relaxed(*tx++, tx_reg);
  673. }
  674. if (rx != NULL) {
  675. if (mcspi_wait_for_reg_bit(chstat_reg,
  676. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  677. dev_err(&spi->dev, "RXS timed out\n");
  678. goto out;
  679. }
  680. if (c == 4 && tx == NULL &&
  681. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  682. omap2_mcspi_set_enable(spi, 0);
  683. *rx++ = readl_relaxed(rx_reg);
  684. dev_vdbg(&spi->dev, "read-%d %08x\n",
  685. word_len, *(rx - 1));
  686. if (mcspi_wait_for_reg_bit(chstat_reg,
  687. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  688. dev_err(&spi->dev,
  689. "RXS timed out\n");
  690. goto out;
  691. }
  692. c = 0;
  693. } else if (c == 0 && tx == NULL) {
  694. omap2_mcspi_set_enable(spi, 0);
  695. }
  696. *rx++ = readl_relaxed(rx_reg);
  697. dev_vdbg(&spi->dev, "read-%d %08x\n",
  698. word_len, *(rx - 1));
  699. }
  700. } while (c >= 4);
  701. }
  702. /* for TX_ONLY mode, be sure all words have shifted out */
  703. if (xfer->rx_buf == NULL) {
  704. if (mcspi_wait_for_reg_bit(chstat_reg,
  705. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  706. dev_err(&spi->dev, "TXS timed out\n");
  707. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  708. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  709. dev_err(&spi->dev, "EOT timed out\n");
  710. /* disable chan to purge rx datas received in TX_ONLY transfer,
  711. * otherwise these rx datas will affect the direct following
  712. * RX_ONLY transfer.
  713. */
  714. omap2_mcspi_set_enable(spi, 0);
  715. }
  716. out:
  717. omap2_mcspi_set_enable(spi, 1);
  718. return count - c;
  719. }
  720. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  721. {
  722. u32 div;
  723. for (div = 0; div < 15; div++)
  724. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  725. return div;
  726. return 15;
  727. }
  728. /* called only when no transfer is active to this device */
  729. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  730. struct spi_transfer *t)
  731. {
  732. struct omap2_mcspi_cs *cs = spi->controller_state;
  733. struct omap2_mcspi *mcspi;
  734. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  735. u8 word_len = spi->bits_per_word;
  736. u32 speed_hz = spi->max_speed_hz;
  737. mcspi = spi_master_get_devdata(spi->master);
  738. if (t != NULL && t->bits_per_word)
  739. word_len = t->bits_per_word;
  740. cs->word_len = word_len;
  741. if (t && t->speed_hz)
  742. speed_hz = t->speed_hz;
  743. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  744. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  745. clkd = omap2_mcspi_calc_divisor(speed_hz);
  746. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  747. clkg = 0;
  748. } else {
  749. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  750. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  751. clkd = (div - 1) & 0xf;
  752. extclk = (div - 1) >> 4;
  753. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  754. }
  755. l = mcspi_cached_chconf0(spi);
  756. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  757. * REVISIT: this controller could support SPI_3WIRE mode.
  758. */
  759. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  760. l &= ~OMAP2_MCSPI_CHCONF_IS;
  761. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  762. l |= OMAP2_MCSPI_CHCONF_DPE0;
  763. } else {
  764. l |= OMAP2_MCSPI_CHCONF_IS;
  765. l |= OMAP2_MCSPI_CHCONF_DPE1;
  766. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  767. }
  768. /* wordlength */
  769. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  770. l |= (word_len - 1) << 7;
  771. /* set chipselect polarity; manage with FORCE */
  772. if (!(spi->mode & SPI_CS_HIGH))
  773. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  774. else
  775. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  776. /* set clock divisor */
  777. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  778. l |= clkd << 2;
  779. /* set clock granularity */
  780. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  781. l |= clkg;
  782. if (clkg) {
  783. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  784. cs->chctrl0 |= extclk << 8;
  785. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  786. }
  787. /* set SPI mode 0..3 */
  788. if (spi->mode & SPI_CPOL)
  789. l |= OMAP2_MCSPI_CHCONF_POL;
  790. else
  791. l &= ~OMAP2_MCSPI_CHCONF_POL;
  792. if (spi->mode & SPI_CPHA)
  793. l |= OMAP2_MCSPI_CHCONF_PHA;
  794. else
  795. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  796. mcspi_write_chconf0(spi, l);
  797. cs->mode = spi->mode;
  798. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  799. speed_hz,
  800. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  801. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  802. return 0;
  803. }
  804. /*
  805. * Note that we currently allow DMA only if we get a channel
  806. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  807. */
  808. static int omap2_mcspi_request_dma(struct spi_device *spi)
  809. {
  810. struct spi_master *master = spi->master;
  811. struct omap2_mcspi *mcspi;
  812. struct omap2_mcspi_dma *mcspi_dma;
  813. int ret = 0;
  814. mcspi = spi_master_get_devdata(master);
  815. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  816. init_completion(&mcspi_dma->dma_rx_completion);
  817. init_completion(&mcspi_dma->dma_tx_completion);
  818. mcspi_dma->dma_rx = dma_request_chan(&master->dev,
  819. mcspi_dma->dma_rx_ch_name);
  820. if (IS_ERR(mcspi_dma->dma_rx)) {
  821. ret = PTR_ERR(mcspi_dma->dma_rx);
  822. mcspi_dma->dma_rx = NULL;
  823. goto no_dma;
  824. }
  825. mcspi_dma->dma_tx = dma_request_chan(&master->dev,
  826. mcspi_dma->dma_tx_ch_name);
  827. if (IS_ERR(mcspi_dma->dma_tx)) {
  828. ret = PTR_ERR(mcspi_dma->dma_tx);
  829. mcspi_dma->dma_tx = NULL;
  830. dma_release_channel(mcspi_dma->dma_rx);
  831. mcspi_dma->dma_rx = NULL;
  832. }
  833. no_dma:
  834. return ret;
  835. }
  836. static int omap2_mcspi_setup(struct spi_device *spi)
  837. {
  838. int ret;
  839. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  840. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  841. struct omap2_mcspi_dma *mcspi_dma;
  842. struct omap2_mcspi_cs *cs = spi->controller_state;
  843. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  844. if (!cs) {
  845. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  846. if (!cs)
  847. return -ENOMEM;
  848. cs->base = mcspi->base + spi->chip_select * 0x14;
  849. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  850. cs->mode = 0;
  851. cs->chconf0 = 0;
  852. cs->chctrl0 = 0;
  853. spi->controller_state = cs;
  854. /* Link this to context save list */
  855. list_add_tail(&cs->node, &ctx->cs);
  856. if (gpio_is_valid(spi->cs_gpio)) {
  857. ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
  858. if (ret) {
  859. dev_err(&spi->dev, "failed to request gpio\n");
  860. return ret;
  861. }
  862. gpio_direction_output(spi->cs_gpio,
  863. !(spi->mode & SPI_CS_HIGH));
  864. }
  865. }
  866. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  867. ret = omap2_mcspi_request_dma(spi);
  868. if (ret)
  869. dev_warn(&spi->dev, "not using DMA for McSPI (%d)\n",
  870. ret);
  871. }
  872. ret = pm_runtime_get_sync(mcspi->dev);
  873. if (ret < 0) {
  874. pm_runtime_put_noidle(mcspi->dev);
  875. return ret;
  876. }
  877. ret = omap2_mcspi_setup_transfer(spi, NULL);
  878. pm_runtime_mark_last_busy(mcspi->dev);
  879. pm_runtime_put_autosuspend(mcspi->dev);
  880. return ret;
  881. }
  882. static void omap2_mcspi_cleanup(struct spi_device *spi)
  883. {
  884. struct omap2_mcspi *mcspi;
  885. struct omap2_mcspi_dma *mcspi_dma;
  886. struct omap2_mcspi_cs *cs;
  887. mcspi = spi_master_get_devdata(spi->master);
  888. if (spi->controller_state) {
  889. /* Unlink controller state from context save list */
  890. cs = spi->controller_state;
  891. list_del(&cs->node);
  892. kfree(cs);
  893. }
  894. if (spi->chip_select < spi->master->num_chipselect) {
  895. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  896. if (mcspi_dma->dma_rx) {
  897. dma_release_channel(mcspi_dma->dma_rx);
  898. mcspi_dma->dma_rx = NULL;
  899. }
  900. if (mcspi_dma->dma_tx) {
  901. dma_release_channel(mcspi_dma->dma_tx);
  902. mcspi_dma->dma_tx = NULL;
  903. }
  904. }
  905. if (gpio_is_valid(spi->cs_gpio))
  906. gpio_free(spi->cs_gpio);
  907. }
  908. static int omap2_mcspi_transfer_one(struct spi_master *master,
  909. struct spi_device *spi,
  910. struct spi_transfer *t)
  911. {
  912. /* We only enable one channel at a time -- the one whose message is
  913. * -- although this controller would gladly
  914. * arbitrate among multiple channels. This corresponds to "single
  915. * channel" master mode. As a side effect, we need to manage the
  916. * chipselect with the FORCE bit ... CS != channel enable.
  917. */
  918. struct omap2_mcspi *mcspi;
  919. struct omap2_mcspi_dma *mcspi_dma;
  920. struct omap2_mcspi_cs *cs;
  921. struct omap2_mcspi_device_config *cd;
  922. int par_override = 0;
  923. int status = 0;
  924. u32 chconf;
  925. mcspi = spi_master_get_devdata(master);
  926. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  927. cs = spi->controller_state;
  928. cd = spi->controller_data;
  929. /*
  930. * The slave driver could have changed spi->mode in which case
  931. * it will be different from cs->mode (the current hardware setup).
  932. * If so, set par_override (even though its not a parity issue) so
  933. * omap2_mcspi_setup_transfer will be called to configure the hardware
  934. * with the correct mode on the first iteration of the loop below.
  935. */
  936. if (spi->mode != cs->mode)
  937. par_override = 1;
  938. omap2_mcspi_set_enable(spi, 0);
  939. if (gpio_is_valid(spi->cs_gpio))
  940. omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
  941. if (par_override ||
  942. (t->speed_hz != spi->max_speed_hz) ||
  943. (t->bits_per_word != spi->bits_per_word)) {
  944. par_override = 1;
  945. status = omap2_mcspi_setup_transfer(spi, t);
  946. if (status < 0)
  947. goto out;
  948. if (t->speed_hz == spi->max_speed_hz &&
  949. t->bits_per_word == spi->bits_per_word)
  950. par_override = 0;
  951. }
  952. if (cd && cd->cs_per_word) {
  953. chconf = mcspi->ctx.modulctrl;
  954. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  955. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  956. mcspi->ctx.modulctrl =
  957. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  958. }
  959. chconf = mcspi_cached_chconf0(spi);
  960. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  961. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  962. if (t->tx_buf == NULL)
  963. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  964. else if (t->rx_buf == NULL)
  965. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  966. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  967. /* Turbo mode is for more than one word */
  968. if (t->len > ((cs->word_len + 7) >> 3))
  969. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  970. }
  971. mcspi_write_chconf0(spi, chconf);
  972. if (t->len) {
  973. unsigned count;
  974. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  975. master->cur_msg_mapped &&
  976. master->can_dma(master, spi, t))
  977. omap2_mcspi_set_fifo(spi, t, 1);
  978. omap2_mcspi_set_enable(spi, 1);
  979. /* RX_ONLY mode needs dummy data in TX reg */
  980. if (t->tx_buf == NULL)
  981. writel_relaxed(0, cs->base
  982. + OMAP2_MCSPI_TX0);
  983. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  984. master->cur_msg_mapped &&
  985. master->can_dma(master, spi, t))
  986. count = omap2_mcspi_txrx_dma(spi, t);
  987. else
  988. count = omap2_mcspi_txrx_pio(spi, t);
  989. if (count != t->len) {
  990. status = -EIO;
  991. goto out;
  992. }
  993. }
  994. omap2_mcspi_set_enable(spi, 0);
  995. if (mcspi->fifo_depth > 0)
  996. omap2_mcspi_set_fifo(spi, t, 0);
  997. out:
  998. /* Restore defaults if they were overriden */
  999. if (par_override) {
  1000. par_override = 0;
  1001. status = omap2_mcspi_setup_transfer(spi, NULL);
  1002. }
  1003. if (cd && cd->cs_per_word) {
  1004. chconf = mcspi->ctx.modulctrl;
  1005. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  1006. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1007. mcspi->ctx.modulctrl =
  1008. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1009. }
  1010. omap2_mcspi_set_enable(spi, 0);
  1011. if (gpio_is_valid(spi->cs_gpio))
  1012. omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
  1013. if (mcspi->fifo_depth > 0 && t)
  1014. omap2_mcspi_set_fifo(spi, t, 0);
  1015. return status;
  1016. }
  1017. static int omap2_mcspi_prepare_message(struct spi_master *master,
  1018. struct spi_message *msg)
  1019. {
  1020. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1021. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1022. struct omap2_mcspi_cs *cs;
  1023. /* Only a single channel can have the FORCE bit enabled
  1024. * in its chconf0 register.
  1025. * Scan all channels and disable them except the current one.
  1026. * A FORCE can remain from a last transfer having cs_change enabled
  1027. */
  1028. list_for_each_entry(cs, &ctx->cs, node) {
  1029. if (msg->spi->controller_state == cs)
  1030. continue;
  1031. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
  1032. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1033. writel_relaxed(cs->chconf0,
  1034. cs->base + OMAP2_MCSPI_CHCONF0);
  1035. readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
  1036. }
  1037. }
  1038. return 0;
  1039. }
  1040. static bool omap2_mcspi_can_dma(struct spi_master *master,
  1041. struct spi_device *spi,
  1042. struct spi_transfer *xfer)
  1043. {
  1044. return (xfer->len >= DMA_MIN_BYTES);
  1045. }
  1046. static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  1047. {
  1048. struct spi_master *master = mcspi->master;
  1049. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1050. int ret = 0;
  1051. ret = pm_runtime_get_sync(mcspi->dev);
  1052. if (ret < 0) {
  1053. pm_runtime_put_noidle(mcspi->dev);
  1054. return ret;
  1055. }
  1056. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1057. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1058. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1059. omap2_mcspi_set_master_mode(master);
  1060. pm_runtime_mark_last_busy(mcspi->dev);
  1061. pm_runtime_put_autosuspend(mcspi->dev);
  1062. return 0;
  1063. }
  1064. /*
  1065. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1066. * inactive state when driver was suspend, then force it to inactive state at
  1067. * wake up.
  1068. */
  1069. static int omap_mcspi_runtime_resume(struct device *dev)
  1070. {
  1071. struct spi_master *master = dev_get_drvdata(dev);
  1072. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1073. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1074. struct omap2_mcspi_cs *cs;
  1075. /* McSPI: context restore */
  1076. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  1077. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  1078. list_for_each_entry(cs, &ctx->cs, node) {
  1079. /*
  1080. * We need to toggle CS state for OMAP take this
  1081. * change in account.
  1082. */
  1083. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1084. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1085. writel_relaxed(cs->chconf0,
  1086. cs->base + OMAP2_MCSPI_CHCONF0);
  1087. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1088. writel_relaxed(cs->chconf0,
  1089. cs->base + OMAP2_MCSPI_CHCONF0);
  1090. } else {
  1091. writel_relaxed(cs->chconf0,
  1092. cs->base + OMAP2_MCSPI_CHCONF0);
  1093. }
  1094. }
  1095. return 0;
  1096. }
  1097. static struct omap2_mcspi_platform_config omap2_pdata = {
  1098. .regs_offset = 0,
  1099. };
  1100. static struct omap2_mcspi_platform_config omap4_pdata = {
  1101. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1102. };
  1103. static const struct of_device_id omap_mcspi_of_match[] = {
  1104. {
  1105. .compatible = "ti,omap2-mcspi",
  1106. .data = &omap2_pdata,
  1107. },
  1108. {
  1109. .compatible = "ti,omap4-mcspi",
  1110. .data = &omap4_pdata,
  1111. },
  1112. { },
  1113. };
  1114. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1115. static int omap2_mcspi_probe(struct platform_device *pdev)
  1116. {
  1117. struct spi_master *master;
  1118. const struct omap2_mcspi_platform_config *pdata;
  1119. struct omap2_mcspi *mcspi;
  1120. struct resource *r;
  1121. int status = 0, i;
  1122. u32 regs_offset = 0;
  1123. struct device_node *node = pdev->dev.of_node;
  1124. const struct of_device_id *match;
  1125. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  1126. if (master == NULL) {
  1127. dev_dbg(&pdev->dev, "master allocation failed\n");
  1128. return -ENOMEM;
  1129. }
  1130. /* the spi->mode bits understood by this driver: */
  1131. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1132. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1133. master->setup = omap2_mcspi_setup;
  1134. master->auto_runtime_pm = true;
  1135. master->prepare_message = omap2_mcspi_prepare_message;
  1136. master->can_dma = omap2_mcspi_can_dma;
  1137. master->transfer_one = omap2_mcspi_transfer_one;
  1138. master->set_cs = omap2_mcspi_set_cs;
  1139. master->cleanup = omap2_mcspi_cleanup;
  1140. master->dev.of_node = node;
  1141. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1142. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1143. platform_set_drvdata(pdev, master);
  1144. mcspi = spi_master_get_devdata(master);
  1145. mcspi->master = master;
  1146. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1147. if (match) {
  1148. u32 num_cs = 1; /* default number of chipselect */
  1149. pdata = match->data;
  1150. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1151. master->num_chipselect = num_cs;
  1152. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1153. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1154. } else {
  1155. pdata = dev_get_platdata(&pdev->dev);
  1156. master->num_chipselect = pdata->num_cs;
  1157. mcspi->pin_dir = pdata->pin_dir;
  1158. }
  1159. regs_offset = pdata->regs_offset;
  1160. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1161. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1162. if (IS_ERR(mcspi->base)) {
  1163. status = PTR_ERR(mcspi->base);
  1164. goto free_master;
  1165. }
  1166. mcspi->phys = r->start + regs_offset;
  1167. mcspi->base += regs_offset;
  1168. mcspi->dev = &pdev->dev;
  1169. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1170. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1171. sizeof(struct omap2_mcspi_dma),
  1172. GFP_KERNEL);
  1173. if (mcspi->dma_channels == NULL) {
  1174. status = -ENOMEM;
  1175. goto free_master;
  1176. }
  1177. for (i = 0; i < master->num_chipselect; i++) {
  1178. sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
  1179. sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
  1180. }
  1181. pm_runtime_use_autosuspend(&pdev->dev);
  1182. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1183. pm_runtime_enable(&pdev->dev);
  1184. status = omap2_mcspi_master_setup(mcspi);
  1185. if (status < 0)
  1186. goto disable_pm;
  1187. status = devm_spi_register_master(&pdev->dev, master);
  1188. if (status < 0)
  1189. goto disable_pm;
  1190. return status;
  1191. disable_pm:
  1192. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1193. pm_runtime_put_sync(&pdev->dev);
  1194. pm_runtime_disable(&pdev->dev);
  1195. free_master:
  1196. spi_master_put(master);
  1197. return status;
  1198. }
  1199. static int omap2_mcspi_remove(struct platform_device *pdev)
  1200. {
  1201. struct spi_master *master = platform_get_drvdata(pdev);
  1202. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1203. pm_runtime_dont_use_autosuspend(mcspi->dev);
  1204. pm_runtime_put_sync(mcspi->dev);
  1205. pm_runtime_disable(&pdev->dev);
  1206. return 0;
  1207. }
  1208. /* work with hotplug and coldplug */
  1209. MODULE_ALIAS("platform:omap2_mcspi");
  1210. static int __maybe_unused omap2_mcspi_suspend(struct device *dev)
  1211. {
  1212. struct spi_master *master = dev_get_drvdata(dev);
  1213. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1214. int error;
  1215. error = pinctrl_pm_select_sleep_state(dev);
  1216. if (error)
  1217. dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
  1218. __func__, error);
  1219. error = spi_master_suspend(master);
  1220. if (error)
  1221. dev_warn(mcspi->dev, "%s: master suspend failed: %i\n",
  1222. __func__, error);
  1223. return pm_runtime_force_suspend(dev);
  1224. }
  1225. static int __maybe_unused omap2_mcspi_resume(struct device *dev)
  1226. {
  1227. struct spi_master *master = dev_get_drvdata(dev);
  1228. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1229. int error;
  1230. error = pinctrl_pm_select_default_state(dev);
  1231. if (error)
  1232. dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
  1233. __func__, error);
  1234. error = spi_master_resume(master);
  1235. if (error)
  1236. dev_warn(mcspi->dev, "%s: master resume failed: %i\n",
  1237. __func__, error);
  1238. return pm_runtime_force_resume(dev);
  1239. }
  1240. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1241. SET_SYSTEM_SLEEP_PM_OPS(omap2_mcspi_suspend,
  1242. omap2_mcspi_resume)
  1243. .runtime_resume = omap_mcspi_runtime_resume,
  1244. };
  1245. static struct platform_driver omap2_mcspi_driver = {
  1246. .driver = {
  1247. .name = "omap2_mcspi",
  1248. .pm = &omap2_mcspi_pm_ops,
  1249. .of_match_table = omap_mcspi_of_match,
  1250. },
  1251. .probe = omap2_mcspi_probe,
  1252. .remove = omap2_mcspi_remove,
  1253. };
  1254. module_platform_driver(omap2_mcspi_driver);
  1255. MODULE_LICENSE("GPL");