spi-omap2-mcspi.c 38 KB

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