spi-omap2-mcspi.c 38 KB

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