omap2_mcspi.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/delay.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/err.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include <linux/slab.h>
  35. #include <linux/spi/spi.h>
  36. #include <plat/dma.h>
  37. #include <plat/clock.h>
  38. #include <plat/mcspi.h>
  39. #define OMAP2_MCSPI_MAX_FREQ 48000000
  40. /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
  41. #define OMAP2_MCSPI_MAX_CTRL 4
  42. #define OMAP2_MCSPI_REVISION 0x00
  43. #define OMAP2_MCSPI_SYSCONFIG 0x10
  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. /* per-channel banks, 0x14 bytes each, first is: */
  51. #define OMAP2_MCSPI_CHCONF0 0x2c
  52. #define OMAP2_MCSPI_CHSTAT0 0x30
  53. #define OMAP2_MCSPI_CHCTRL0 0x34
  54. #define OMAP2_MCSPI_TX0 0x38
  55. #define OMAP2_MCSPI_RX0 0x3c
  56. /* per-register bitmasks: */
  57. #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4)
  58. #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
  59. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
  60. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
  61. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE BIT(0)
  62. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  63. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  64. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  65. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  66. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  67. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  68. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  69. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  70. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  71. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  72. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  73. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  74. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  75. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  76. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  77. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  78. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  79. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  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_CHCTRL_EN BIT(0)
  84. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  85. /* We have 2 DMA channels per CS, one for RX and one for TX */
  86. struct omap2_mcspi_dma {
  87. int dma_tx_channel;
  88. int dma_rx_channel;
  89. int dma_tx_sync_dev;
  90. int dma_rx_sync_dev;
  91. struct completion dma_tx_completion;
  92. struct completion dma_rx_completion;
  93. };
  94. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  95. * cache operations; better heuristics consider wordsize and bitrate.
  96. */
  97. #define DMA_MIN_BYTES 160
  98. struct omap2_mcspi {
  99. struct work_struct work;
  100. /* lock protects queue and registers */
  101. spinlock_t lock;
  102. struct list_head msg_queue;
  103. struct spi_master *master;
  104. struct clk *ick;
  105. struct clk *fck;
  106. /* Virtual base address of the controller */
  107. void __iomem *base;
  108. unsigned long phys;
  109. /* SPI1 has 4 channels, while SPI2 has 2 */
  110. struct omap2_mcspi_dma *dma_channels;
  111. };
  112. struct omap2_mcspi_cs {
  113. void __iomem *base;
  114. unsigned long phys;
  115. int word_len;
  116. struct list_head node;
  117. /* Context save and restore shadow register */
  118. u32 chconf0;
  119. };
  120. /* used for context save and restore, structure members to be updated whenever
  121. * corresponding registers are modified.
  122. */
  123. struct omap2_mcspi_regs {
  124. u32 sysconfig;
  125. u32 modulctrl;
  126. u32 wakeupenable;
  127. struct list_head cs;
  128. };
  129. static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
  130. static struct workqueue_struct *omap2_mcspi_wq;
  131. #define MOD_REG_BIT(val, mask, set) do { \
  132. if (set) \
  133. val |= mask; \
  134. else \
  135. val &= ~mask; \
  136. } while (0)
  137. static inline void mcspi_write_reg(struct spi_master *master,
  138. int idx, u32 val)
  139. {
  140. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  141. __raw_writel(val, mcspi->base + idx);
  142. }
  143. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  144. {
  145. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  146. return __raw_readl(mcspi->base + idx);
  147. }
  148. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  149. int idx, u32 val)
  150. {
  151. struct omap2_mcspi_cs *cs = spi->controller_state;
  152. __raw_writel(val, cs->base + idx);
  153. }
  154. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  155. {
  156. struct omap2_mcspi_cs *cs = spi->controller_state;
  157. return __raw_readl(cs->base + idx);
  158. }
  159. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  160. {
  161. struct omap2_mcspi_cs *cs = spi->controller_state;
  162. return cs->chconf0;
  163. }
  164. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  165. {
  166. struct omap2_mcspi_cs *cs = spi->controller_state;
  167. cs->chconf0 = val;
  168. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  169. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  170. }
  171. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  172. int is_read, int enable)
  173. {
  174. u32 l, rw;
  175. l = mcspi_cached_chconf0(spi);
  176. if (is_read) /* 1 is read, 0 write */
  177. rw = OMAP2_MCSPI_CHCONF_DMAR;
  178. else
  179. rw = OMAP2_MCSPI_CHCONF_DMAW;
  180. MOD_REG_BIT(l, rw, enable);
  181. mcspi_write_chconf0(spi, l);
  182. }
  183. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  184. {
  185. u32 l;
  186. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  187. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  188. /* Flash post-writes */
  189. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  190. }
  191. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  192. {
  193. u32 l;
  194. l = mcspi_cached_chconf0(spi);
  195. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  196. mcspi_write_chconf0(spi, l);
  197. }
  198. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  199. {
  200. u32 l;
  201. /* setup when switching from (reset default) slave mode
  202. * to single-channel master mode
  203. */
  204. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  205. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  206. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  207. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  208. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  209. omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
  210. }
  211. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  212. {
  213. struct spi_master *spi_cntrl;
  214. struct omap2_mcspi_cs *cs;
  215. spi_cntrl = mcspi->master;
  216. /* McSPI: context restore */
  217. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
  218. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
  219. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG,
  220. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig);
  221. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
  222. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
  223. list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
  224. node)
  225. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  226. }
  227. static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
  228. {
  229. clk_disable(mcspi->ick);
  230. clk_disable(mcspi->fck);
  231. }
  232. static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
  233. {
  234. if (clk_enable(mcspi->ick))
  235. return -ENODEV;
  236. if (clk_enable(mcspi->fck))
  237. return -ENODEV;
  238. omap2_mcspi_restore_ctx(mcspi);
  239. return 0;
  240. }
  241. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  242. {
  243. unsigned long timeout;
  244. timeout = jiffies + msecs_to_jiffies(1000);
  245. while (!(__raw_readl(reg) & bit)) {
  246. if (time_after(jiffies, timeout))
  247. return -1;
  248. cpu_relax();
  249. }
  250. return 0;
  251. }
  252. static unsigned
  253. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  254. {
  255. struct omap2_mcspi *mcspi;
  256. struct omap2_mcspi_cs *cs = spi->controller_state;
  257. struct omap2_mcspi_dma *mcspi_dma;
  258. unsigned int count, c;
  259. unsigned long base, tx_reg, rx_reg;
  260. int word_len, data_type, element_count;
  261. int elements;
  262. u32 l;
  263. u8 * rx;
  264. const u8 * tx;
  265. void __iomem *chstat_reg;
  266. mcspi = spi_master_get_devdata(spi->master);
  267. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  268. l = mcspi_cached_chconf0(spi);
  269. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  270. count = xfer->len;
  271. c = count;
  272. word_len = cs->word_len;
  273. base = cs->phys;
  274. tx_reg = base + OMAP2_MCSPI_TX0;
  275. rx_reg = base + OMAP2_MCSPI_RX0;
  276. rx = xfer->rx_buf;
  277. tx = xfer->tx_buf;
  278. if (word_len <= 8) {
  279. data_type = OMAP_DMA_DATA_TYPE_S8;
  280. element_count = count;
  281. } else if (word_len <= 16) {
  282. data_type = OMAP_DMA_DATA_TYPE_S16;
  283. element_count = count >> 1;
  284. } else /* word_len <= 32 */ {
  285. data_type = OMAP_DMA_DATA_TYPE_S32;
  286. element_count = count >> 2;
  287. }
  288. if (tx != NULL) {
  289. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  290. data_type, element_count, 1,
  291. OMAP_DMA_SYNC_ELEMENT,
  292. mcspi_dma->dma_tx_sync_dev, 0);
  293. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  294. OMAP_DMA_AMODE_CONSTANT,
  295. tx_reg, 0, 0);
  296. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  297. OMAP_DMA_AMODE_POST_INC,
  298. xfer->tx_dma, 0, 0);
  299. }
  300. if (rx != NULL) {
  301. elements = element_count - 1;
  302. if (l & OMAP2_MCSPI_CHCONF_TURBO)
  303. elements--;
  304. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  305. data_type, elements, 1,
  306. OMAP_DMA_SYNC_ELEMENT,
  307. mcspi_dma->dma_rx_sync_dev, 1);
  308. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  309. OMAP_DMA_AMODE_CONSTANT,
  310. rx_reg, 0, 0);
  311. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  312. OMAP_DMA_AMODE_POST_INC,
  313. xfer->rx_dma, 0, 0);
  314. }
  315. if (tx != NULL) {
  316. omap_start_dma(mcspi_dma->dma_tx_channel);
  317. omap2_mcspi_set_dma_req(spi, 0, 1);
  318. }
  319. if (rx != NULL) {
  320. omap_start_dma(mcspi_dma->dma_rx_channel);
  321. omap2_mcspi_set_dma_req(spi, 1, 1);
  322. }
  323. if (tx != NULL) {
  324. wait_for_completion(&mcspi_dma->dma_tx_completion);
  325. dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
  326. /* for TX_ONLY mode, be sure all words have shifted out */
  327. if (rx == NULL) {
  328. if (mcspi_wait_for_reg_bit(chstat_reg,
  329. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  330. dev_err(&spi->dev, "TXS timed out\n");
  331. else if (mcspi_wait_for_reg_bit(chstat_reg,
  332. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  333. dev_err(&spi->dev, "EOT timed out\n");
  334. }
  335. }
  336. if (rx != NULL) {
  337. wait_for_completion(&mcspi_dma->dma_rx_completion);
  338. dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
  339. omap2_mcspi_set_enable(spi, 0);
  340. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  341. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  342. & OMAP2_MCSPI_CHSTAT_RXS)) {
  343. u32 w;
  344. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  345. if (word_len <= 8)
  346. ((u8 *)xfer->rx_buf)[elements++] = w;
  347. else if (word_len <= 16)
  348. ((u16 *)xfer->rx_buf)[elements++] = w;
  349. else /* word_len <= 32 */
  350. ((u32 *)xfer->rx_buf)[elements++] = w;
  351. } else {
  352. dev_err(&spi->dev,
  353. "DMA RX penultimate word empty");
  354. count -= (word_len <= 8) ? 2 :
  355. (word_len <= 16) ? 4 :
  356. /* word_len <= 32 */ 8;
  357. omap2_mcspi_set_enable(spi, 1);
  358. return count;
  359. }
  360. }
  361. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  362. & OMAP2_MCSPI_CHSTAT_RXS)) {
  363. u32 w;
  364. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  365. if (word_len <= 8)
  366. ((u8 *)xfer->rx_buf)[elements] = w;
  367. else if (word_len <= 16)
  368. ((u16 *)xfer->rx_buf)[elements] = w;
  369. else /* word_len <= 32 */
  370. ((u32 *)xfer->rx_buf)[elements] = w;
  371. } else {
  372. dev_err(&spi->dev, "DMA RX last word empty");
  373. count -= (word_len <= 8) ? 1 :
  374. (word_len <= 16) ? 2 :
  375. /* word_len <= 32 */ 4;
  376. }
  377. omap2_mcspi_set_enable(spi, 1);
  378. }
  379. return count;
  380. }
  381. static unsigned
  382. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  383. {
  384. struct omap2_mcspi *mcspi;
  385. struct omap2_mcspi_cs *cs = spi->controller_state;
  386. unsigned int count, c;
  387. u32 l;
  388. void __iomem *base = cs->base;
  389. void __iomem *tx_reg;
  390. void __iomem *rx_reg;
  391. void __iomem *chstat_reg;
  392. int word_len;
  393. mcspi = spi_master_get_devdata(spi->master);
  394. count = xfer->len;
  395. c = count;
  396. word_len = cs->word_len;
  397. l = mcspi_cached_chconf0(spi);
  398. /* We store the pre-calculated register addresses on stack to speed
  399. * up the transfer loop. */
  400. tx_reg = base + OMAP2_MCSPI_TX0;
  401. rx_reg = base + OMAP2_MCSPI_RX0;
  402. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  403. if (c < (word_len>>3))
  404. return 0;
  405. if (word_len <= 8) {
  406. u8 *rx;
  407. const u8 *tx;
  408. rx = xfer->rx_buf;
  409. tx = xfer->tx_buf;
  410. do {
  411. c -= 1;
  412. if (tx != NULL) {
  413. if (mcspi_wait_for_reg_bit(chstat_reg,
  414. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  415. dev_err(&spi->dev, "TXS timed out\n");
  416. goto out;
  417. }
  418. dev_vdbg(&spi->dev, "write-%d %02x\n",
  419. word_len, *tx);
  420. __raw_writel(*tx++, tx_reg);
  421. }
  422. if (rx != NULL) {
  423. if (mcspi_wait_for_reg_bit(chstat_reg,
  424. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  425. dev_err(&spi->dev, "RXS timed out\n");
  426. goto out;
  427. }
  428. if (c == 1 && tx == NULL &&
  429. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  430. omap2_mcspi_set_enable(spi, 0);
  431. *rx++ = __raw_readl(rx_reg);
  432. dev_vdbg(&spi->dev, "read-%d %02x\n",
  433. word_len, *(rx - 1));
  434. if (mcspi_wait_for_reg_bit(chstat_reg,
  435. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  436. dev_err(&spi->dev,
  437. "RXS timed out\n");
  438. goto out;
  439. }
  440. c = 0;
  441. } else if (c == 0 && tx == NULL) {
  442. omap2_mcspi_set_enable(spi, 0);
  443. }
  444. *rx++ = __raw_readl(rx_reg);
  445. dev_vdbg(&spi->dev, "read-%d %02x\n",
  446. word_len, *(rx - 1));
  447. }
  448. } while (c > (word_len>>3));
  449. } else if (word_len <= 16) {
  450. u16 *rx;
  451. const u16 *tx;
  452. rx = xfer->rx_buf;
  453. tx = xfer->tx_buf;
  454. do {
  455. c -= 2;
  456. if (tx != NULL) {
  457. if (mcspi_wait_for_reg_bit(chstat_reg,
  458. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  459. dev_err(&spi->dev, "TXS timed out\n");
  460. goto out;
  461. }
  462. dev_vdbg(&spi->dev, "write-%d %04x\n",
  463. word_len, *tx);
  464. __raw_writel(*tx++, tx_reg);
  465. }
  466. if (rx != NULL) {
  467. if (mcspi_wait_for_reg_bit(chstat_reg,
  468. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  469. dev_err(&spi->dev, "RXS timed out\n");
  470. goto out;
  471. }
  472. if (c == 2 && tx == NULL &&
  473. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  474. omap2_mcspi_set_enable(spi, 0);
  475. *rx++ = __raw_readl(rx_reg);
  476. dev_vdbg(&spi->dev, "read-%d %04x\n",
  477. word_len, *(rx - 1));
  478. if (mcspi_wait_for_reg_bit(chstat_reg,
  479. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  480. dev_err(&spi->dev,
  481. "RXS timed out\n");
  482. goto out;
  483. }
  484. c = 0;
  485. } else if (c == 0 && tx == NULL) {
  486. omap2_mcspi_set_enable(spi, 0);
  487. }
  488. *rx++ = __raw_readl(rx_reg);
  489. dev_vdbg(&spi->dev, "read-%d %04x\n",
  490. word_len, *(rx - 1));
  491. }
  492. } while (c > (word_len>>3));
  493. } else if (word_len <= 32) {
  494. u32 *rx;
  495. const u32 *tx;
  496. rx = xfer->rx_buf;
  497. tx = xfer->tx_buf;
  498. do {
  499. c -= 4;
  500. if (tx != NULL) {
  501. if (mcspi_wait_for_reg_bit(chstat_reg,
  502. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  503. dev_err(&spi->dev, "TXS timed out\n");
  504. goto out;
  505. }
  506. dev_vdbg(&spi->dev, "write-%d %08x\n",
  507. word_len, *tx);
  508. __raw_writel(*tx++, tx_reg);
  509. }
  510. if (rx != NULL) {
  511. if (mcspi_wait_for_reg_bit(chstat_reg,
  512. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  513. dev_err(&spi->dev, "RXS timed out\n");
  514. goto out;
  515. }
  516. if (c == 4 && tx == NULL &&
  517. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  518. omap2_mcspi_set_enable(spi, 0);
  519. *rx++ = __raw_readl(rx_reg);
  520. dev_vdbg(&spi->dev, "read-%d %08x\n",
  521. word_len, *(rx - 1));
  522. if (mcspi_wait_for_reg_bit(chstat_reg,
  523. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  524. dev_err(&spi->dev,
  525. "RXS timed out\n");
  526. goto out;
  527. }
  528. c = 0;
  529. } else if (c == 0 && tx == NULL) {
  530. omap2_mcspi_set_enable(spi, 0);
  531. }
  532. *rx++ = __raw_readl(rx_reg);
  533. dev_vdbg(&spi->dev, "read-%d %08x\n",
  534. word_len, *(rx - 1));
  535. }
  536. } while (c > (word_len>>3));
  537. }
  538. /* for TX_ONLY mode, be sure all words have shifted out */
  539. if (xfer->rx_buf == NULL) {
  540. if (mcspi_wait_for_reg_bit(chstat_reg,
  541. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  542. dev_err(&spi->dev, "TXS timed out\n");
  543. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  544. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  545. dev_err(&spi->dev, "EOT timed out\n");
  546. /* disable chan to purge rx datas received in TX_ONLY transfer,
  547. * otherwise these rx datas will affect the direct following
  548. * RX_ONLY transfer.
  549. */
  550. omap2_mcspi_set_enable(spi, 0);
  551. }
  552. out:
  553. omap2_mcspi_set_enable(spi, 1);
  554. return count - c;
  555. }
  556. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  557. {
  558. u32 div;
  559. for (div = 0; div < 15; div++)
  560. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  561. return div;
  562. return 15;
  563. }
  564. /* called only when no transfer is active to this device */
  565. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  566. struct spi_transfer *t)
  567. {
  568. struct omap2_mcspi_cs *cs = spi->controller_state;
  569. struct omap2_mcspi *mcspi;
  570. struct spi_master *spi_cntrl;
  571. u32 l = 0, div = 0;
  572. u8 word_len = spi->bits_per_word;
  573. u32 speed_hz = spi->max_speed_hz;
  574. mcspi = spi_master_get_devdata(spi->master);
  575. spi_cntrl = mcspi->master;
  576. if (t != NULL && t->bits_per_word)
  577. word_len = t->bits_per_word;
  578. cs->word_len = word_len;
  579. if (t && t->speed_hz)
  580. speed_hz = t->speed_hz;
  581. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  582. div = omap2_mcspi_calc_divisor(speed_hz);
  583. l = mcspi_cached_chconf0(spi);
  584. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  585. * REVISIT: this controller could support SPI_3WIRE mode.
  586. */
  587. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  588. l |= OMAP2_MCSPI_CHCONF_DPE0;
  589. /* wordlength */
  590. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  591. l |= (word_len - 1) << 7;
  592. /* set chipselect polarity; manage with FORCE */
  593. if (!(spi->mode & SPI_CS_HIGH))
  594. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  595. else
  596. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  597. /* set clock divisor */
  598. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  599. l |= div << 2;
  600. /* set SPI mode 0..3 */
  601. if (spi->mode & SPI_CPOL)
  602. l |= OMAP2_MCSPI_CHCONF_POL;
  603. else
  604. l &= ~OMAP2_MCSPI_CHCONF_POL;
  605. if (spi->mode & SPI_CPHA)
  606. l |= OMAP2_MCSPI_CHCONF_PHA;
  607. else
  608. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  609. mcspi_write_chconf0(spi, l);
  610. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  611. OMAP2_MCSPI_MAX_FREQ >> div,
  612. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  613. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  614. return 0;
  615. }
  616. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  617. {
  618. struct spi_device *spi = data;
  619. struct omap2_mcspi *mcspi;
  620. struct omap2_mcspi_dma *mcspi_dma;
  621. mcspi = spi_master_get_devdata(spi->master);
  622. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  623. complete(&mcspi_dma->dma_rx_completion);
  624. /* We must disable the DMA RX request */
  625. omap2_mcspi_set_dma_req(spi, 1, 0);
  626. }
  627. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  628. {
  629. struct spi_device *spi = data;
  630. struct omap2_mcspi *mcspi;
  631. struct omap2_mcspi_dma *mcspi_dma;
  632. mcspi = spi_master_get_devdata(spi->master);
  633. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  634. complete(&mcspi_dma->dma_tx_completion);
  635. /* We must disable the DMA TX request */
  636. omap2_mcspi_set_dma_req(spi, 0, 0);
  637. }
  638. static int omap2_mcspi_request_dma(struct spi_device *spi)
  639. {
  640. struct spi_master *master = spi->master;
  641. struct omap2_mcspi *mcspi;
  642. struct omap2_mcspi_dma *mcspi_dma;
  643. mcspi = spi_master_get_devdata(master);
  644. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  645. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  646. omap2_mcspi_dma_rx_callback, spi,
  647. &mcspi_dma->dma_rx_channel)) {
  648. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  649. return -EAGAIN;
  650. }
  651. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  652. omap2_mcspi_dma_tx_callback, spi,
  653. &mcspi_dma->dma_tx_channel)) {
  654. omap_free_dma(mcspi_dma->dma_rx_channel);
  655. mcspi_dma->dma_rx_channel = -1;
  656. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  657. return -EAGAIN;
  658. }
  659. init_completion(&mcspi_dma->dma_rx_completion);
  660. init_completion(&mcspi_dma->dma_tx_completion);
  661. return 0;
  662. }
  663. static int omap2_mcspi_setup(struct spi_device *spi)
  664. {
  665. int ret;
  666. struct omap2_mcspi *mcspi;
  667. struct omap2_mcspi_dma *mcspi_dma;
  668. struct omap2_mcspi_cs *cs = spi->controller_state;
  669. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  670. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  671. spi->bits_per_word);
  672. return -EINVAL;
  673. }
  674. mcspi = spi_master_get_devdata(spi->master);
  675. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  676. if (!cs) {
  677. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  678. if (!cs)
  679. return -ENOMEM;
  680. cs->base = mcspi->base + spi->chip_select * 0x14;
  681. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  682. cs->chconf0 = 0;
  683. spi->controller_state = cs;
  684. /* Link this to context save list */
  685. list_add_tail(&cs->node,
  686. &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
  687. }
  688. if (mcspi_dma->dma_rx_channel == -1
  689. || mcspi_dma->dma_tx_channel == -1) {
  690. ret = omap2_mcspi_request_dma(spi);
  691. if (ret < 0)
  692. return ret;
  693. }
  694. if (omap2_mcspi_enable_clocks(mcspi))
  695. return -ENODEV;
  696. ret = omap2_mcspi_setup_transfer(spi, NULL);
  697. omap2_mcspi_disable_clocks(mcspi);
  698. return ret;
  699. }
  700. static void omap2_mcspi_cleanup(struct spi_device *spi)
  701. {
  702. struct omap2_mcspi *mcspi;
  703. struct omap2_mcspi_dma *mcspi_dma;
  704. struct omap2_mcspi_cs *cs;
  705. mcspi = spi_master_get_devdata(spi->master);
  706. if (spi->controller_state) {
  707. /* Unlink controller state from context save list */
  708. cs = spi->controller_state;
  709. list_del(&cs->node);
  710. kfree(spi->controller_state);
  711. }
  712. if (spi->chip_select < spi->master->num_chipselect) {
  713. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  714. if (mcspi_dma->dma_rx_channel != -1) {
  715. omap_free_dma(mcspi_dma->dma_rx_channel);
  716. mcspi_dma->dma_rx_channel = -1;
  717. }
  718. if (mcspi_dma->dma_tx_channel != -1) {
  719. omap_free_dma(mcspi_dma->dma_tx_channel);
  720. mcspi_dma->dma_tx_channel = -1;
  721. }
  722. }
  723. }
  724. static void omap2_mcspi_work(struct work_struct *work)
  725. {
  726. struct omap2_mcspi *mcspi;
  727. mcspi = container_of(work, struct omap2_mcspi, work);
  728. spin_lock_irq(&mcspi->lock);
  729. if (omap2_mcspi_enable_clocks(mcspi))
  730. goto out;
  731. /* We only enable one channel at a time -- the one whose message is
  732. * at the head of the queue -- although this controller would gladly
  733. * arbitrate among multiple channels. This corresponds to "single
  734. * channel" master mode. As a side effect, we need to manage the
  735. * chipselect with the FORCE bit ... CS != channel enable.
  736. */
  737. while (!list_empty(&mcspi->msg_queue)) {
  738. struct spi_message *m;
  739. struct spi_device *spi;
  740. struct spi_transfer *t = NULL;
  741. int cs_active = 0;
  742. struct omap2_mcspi_cs *cs;
  743. struct omap2_mcspi_device_config *cd;
  744. int par_override = 0;
  745. int status = 0;
  746. u32 chconf;
  747. m = container_of(mcspi->msg_queue.next, struct spi_message,
  748. queue);
  749. list_del_init(&m->queue);
  750. spin_unlock_irq(&mcspi->lock);
  751. spi = m->spi;
  752. cs = spi->controller_state;
  753. cd = spi->controller_data;
  754. omap2_mcspi_set_enable(spi, 1);
  755. list_for_each_entry(t, &m->transfers, transfer_list) {
  756. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  757. status = -EINVAL;
  758. break;
  759. }
  760. if (par_override || t->speed_hz || t->bits_per_word) {
  761. par_override = 1;
  762. status = omap2_mcspi_setup_transfer(spi, t);
  763. if (status < 0)
  764. break;
  765. if (!t->speed_hz && !t->bits_per_word)
  766. par_override = 0;
  767. }
  768. if (!cs_active) {
  769. omap2_mcspi_force_cs(spi, 1);
  770. cs_active = 1;
  771. }
  772. chconf = mcspi_cached_chconf0(spi);
  773. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  774. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  775. if (t->tx_buf == NULL)
  776. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  777. else if (t->rx_buf == NULL)
  778. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  779. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  780. /* Turbo mode is for more than one word */
  781. if (t->len > ((cs->word_len + 7) >> 3))
  782. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  783. }
  784. mcspi_write_chconf0(spi, chconf);
  785. if (t->len) {
  786. unsigned count;
  787. /* RX_ONLY mode needs dummy data in TX reg */
  788. if (t->tx_buf == NULL)
  789. __raw_writel(0, cs->base
  790. + OMAP2_MCSPI_TX0);
  791. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  792. count = omap2_mcspi_txrx_dma(spi, t);
  793. else
  794. count = omap2_mcspi_txrx_pio(spi, t);
  795. m->actual_length += count;
  796. if (count != t->len) {
  797. status = -EIO;
  798. break;
  799. }
  800. }
  801. if (t->delay_usecs)
  802. udelay(t->delay_usecs);
  803. /* ignore the "leave it on after last xfer" hint */
  804. if (t->cs_change) {
  805. omap2_mcspi_force_cs(spi, 0);
  806. cs_active = 0;
  807. }
  808. }
  809. /* Restore defaults if they were overriden */
  810. if (par_override) {
  811. par_override = 0;
  812. status = omap2_mcspi_setup_transfer(spi, NULL);
  813. }
  814. if (cs_active)
  815. omap2_mcspi_force_cs(spi, 0);
  816. omap2_mcspi_set_enable(spi, 0);
  817. m->status = status;
  818. m->complete(m->context);
  819. spin_lock_irq(&mcspi->lock);
  820. }
  821. omap2_mcspi_disable_clocks(mcspi);
  822. out:
  823. spin_unlock_irq(&mcspi->lock);
  824. }
  825. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  826. {
  827. struct omap2_mcspi *mcspi;
  828. unsigned long flags;
  829. struct spi_transfer *t;
  830. m->actual_length = 0;
  831. m->status = 0;
  832. /* reject invalid messages and transfers */
  833. if (list_empty(&m->transfers) || !m->complete)
  834. return -EINVAL;
  835. list_for_each_entry(t, &m->transfers, transfer_list) {
  836. const void *tx_buf = t->tx_buf;
  837. void *rx_buf = t->rx_buf;
  838. unsigned len = t->len;
  839. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  840. || (len && !(rx_buf || tx_buf))
  841. || (t->bits_per_word &&
  842. ( t->bits_per_word < 4
  843. || t->bits_per_word > 32))) {
  844. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  845. t->speed_hz,
  846. len,
  847. tx_buf ? "tx" : "",
  848. rx_buf ? "rx" : "",
  849. t->bits_per_word);
  850. return -EINVAL;
  851. }
  852. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  853. dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n",
  854. t->speed_hz,
  855. OMAP2_MCSPI_MAX_FREQ >> 15);
  856. return -EINVAL;
  857. }
  858. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  859. continue;
  860. if (tx_buf != NULL) {
  861. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  862. len, DMA_TO_DEVICE);
  863. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  864. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  865. 'T', len);
  866. return -EINVAL;
  867. }
  868. }
  869. if (rx_buf != NULL) {
  870. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  871. DMA_FROM_DEVICE);
  872. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  873. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  874. 'R', len);
  875. if (tx_buf != NULL)
  876. dma_unmap_single(&spi->dev, t->tx_dma,
  877. len, DMA_TO_DEVICE);
  878. return -EINVAL;
  879. }
  880. }
  881. }
  882. mcspi = spi_master_get_devdata(spi->master);
  883. spin_lock_irqsave(&mcspi->lock, flags);
  884. list_add_tail(&m->queue, &mcspi->msg_queue);
  885. queue_work(omap2_mcspi_wq, &mcspi->work);
  886. spin_unlock_irqrestore(&mcspi->lock, flags);
  887. return 0;
  888. }
  889. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  890. {
  891. struct spi_master *master = mcspi->master;
  892. u32 tmp;
  893. if (omap2_mcspi_enable_clocks(mcspi))
  894. return -1;
  895. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  896. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  897. do {
  898. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  899. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  900. tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
  901. OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
  902. OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
  903. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
  904. omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
  905. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  906. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  907. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  908. omap2_mcspi_set_master_mode(master);
  909. omap2_mcspi_disable_clocks(mcspi);
  910. return 0;
  911. }
  912. static u8 __initdata spi1_rxdma_id [] = {
  913. OMAP24XX_DMA_SPI1_RX0,
  914. OMAP24XX_DMA_SPI1_RX1,
  915. OMAP24XX_DMA_SPI1_RX2,
  916. OMAP24XX_DMA_SPI1_RX3,
  917. };
  918. static u8 __initdata spi1_txdma_id [] = {
  919. OMAP24XX_DMA_SPI1_TX0,
  920. OMAP24XX_DMA_SPI1_TX1,
  921. OMAP24XX_DMA_SPI1_TX2,
  922. OMAP24XX_DMA_SPI1_TX3,
  923. };
  924. static u8 __initdata spi2_rxdma_id[] = {
  925. OMAP24XX_DMA_SPI2_RX0,
  926. OMAP24XX_DMA_SPI2_RX1,
  927. };
  928. static u8 __initdata spi2_txdma_id[] = {
  929. OMAP24XX_DMA_SPI2_TX0,
  930. OMAP24XX_DMA_SPI2_TX1,
  931. };
  932. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  933. || defined(CONFIG_ARCH_OMAP4)
  934. static u8 __initdata spi3_rxdma_id[] = {
  935. OMAP24XX_DMA_SPI3_RX0,
  936. OMAP24XX_DMA_SPI3_RX1,
  937. };
  938. static u8 __initdata spi3_txdma_id[] = {
  939. OMAP24XX_DMA_SPI3_TX0,
  940. OMAP24XX_DMA_SPI3_TX1,
  941. };
  942. #endif
  943. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  944. static u8 __initdata spi4_rxdma_id[] = {
  945. OMAP34XX_DMA_SPI4_RX0,
  946. };
  947. static u8 __initdata spi4_txdma_id[] = {
  948. OMAP34XX_DMA_SPI4_TX0,
  949. };
  950. #endif
  951. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  952. {
  953. struct spi_master *master;
  954. struct omap2_mcspi *mcspi;
  955. struct resource *r;
  956. int status = 0, i;
  957. const u8 *rxdma_id, *txdma_id;
  958. unsigned num_chipselect;
  959. switch (pdev->id) {
  960. case 1:
  961. rxdma_id = spi1_rxdma_id;
  962. txdma_id = spi1_txdma_id;
  963. num_chipselect = 4;
  964. break;
  965. case 2:
  966. rxdma_id = spi2_rxdma_id;
  967. txdma_id = spi2_txdma_id;
  968. num_chipselect = 2;
  969. break;
  970. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  971. || defined(CONFIG_ARCH_OMAP4)
  972. case 3:
  973. rxdma_id = spi3_rxdma_id;
  974. txdma_id = spi3_txdma_id;
  975. num_chipselect = 2;
  976. break;
  977. #endif
  978. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  979. case 4:
  980. rxdma_id = spi4_rxdma_id;
  981. txdma_id = spi4_txdma_id;
  982. num_chipselect = 1;
  983. break;
  984. #endif
  985. default:
  986. return -EINVAL;
  987. }
  988. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  989. if (master == NULL) {
  990. dev_dbg(&pdev->dev, "master allocation failed\n");
  991. return -ENOMEM;
  992. }
  993. /* the spi->mode bits understood by this driver: */
  994. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  995. if (pdev->id != -1)
  996. master->bus_num = pdev->id;
  997. master->setup = omap2_mcspi_setup;
  998. master->transfer = omap2_mcspi_transfer;
  999. master->cleanup = omap2_mcspi_cleanup;
  1000. master->num_chipselect = num_chipselect;
  1001. dev_set_drvdata(&pdev->dev, master);
  1002. mcspi = spi_master_get_devdata(master);
  1003. mcspi->master = master;
  1004. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1005. if (r == NULL) {
  1006. status = -ENODEV;
  1007. goto err1;
  1008. }
  1009. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  1010. dev_name(&pdev->dev))) {
  1011. status = -EBUSY;
  1012. goto err1;
  1013. }
  1014. mcspi->phys = r->start;
  1015. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  1016. if (!mcspi->base) {
  1017. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  1018. status = -ENOMEM;
  1019. goto err1aa;
  1020. }
  1021. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  1022. spin_lock_init(&mcspi->lock);
  1023. INIT_LIST_HEAD(&mcspi->msg_queue);
  1024. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  1025. mcspi->ick = clk_get(&pdev->dev, "ick");
  1026. if (IS_ERR(mcspi->ick)) {
  1027. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  1028. status = PTR_ERR(mcspi->ick);
  1029. goto err1a;
  1030. }
  1031. mcspi->fck = clk_get(&pdev->dev, "fck");
  1032. if (IS_ERR(mcspi->fck)) {
  1033. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  1034. status = PTR_ERR(mcspi->fck);
  1035. goto err2;
  1036. }
  1037. mcspi->dma_channels = kcalloc(master->num_chipselect,
  1038. sizeof(struct omap2_mcspi_dma),
  1039. GFP_KERNEL);
  1040. if (mcspi->dma_channels == NULL)
  1041. goto err3;
  1042. for (i = 0; i < num_chipselect; i++) {
  1043. mcspi->dma_channels[i].dma_rx_channel = -1;
  1044. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  1045. mcspi->dma_channels[i].dma_tx_channel = -1;
  1046. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  1047. }
  1048. if (omap2_mcspi_reset(mcspi) < 0)
  1049. goto err4;
  1050. status = spi_register_master(master);
  1051. if (status < 0)
  1052. goto err4;
  1053. return status;
  1054. err4:
  1055. kfree(mcspi->dma_channels);
  1056. err3:
  1057. clk_put(mcspi->fck);
  1058. err2:
  1059. clk_put(mcspi->ick);
  1060. err1a:
  1061. iounmap(mcspi->base);
  1062. err1aa:
  1063. release_mem_region(r->start, (r->end - r->start) + 1);
  1064. err1:
  1065. spi_master_put(master);
  1066. return status;
  1067. }
  1068. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  1069. {
  1070. struct spi_master *master;
  1071. struct omap2_mcspi *mcspi;
  1072. struct omap2_mcspi_dma *dma_channels;
  1073. struct resource *r;
  1074. void __iomem *base;
  1075. master = dev_get_drvdata(&pdev->dev);
  1076. mcspi = spi_master_get_devdata(master);
  1077. dma_channels = mcspi->dma_channels;
  1078. clk_put(mcspi->fck);
  1079. clk_put(mcspi->ick);
  1080. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1081. release_mem_region(r->start, (r->end - r->start) + 1);
  1082. base = mcspi->base;
  1083. spi_unregister_master(master);
  1084. iounmap(base);
  1085. kfree(dma_channels);
  1086. return 0;
  1087. }
  1088. /* work with hotplug and coldplug */
  1089. MODULE_ALIAS("platform:omap2_mcspi");
  1090. #ifdef CONFIG_SUSPEND
  1091. /*
  1092. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1093. * unactive state when driver was suspend, then force it to unactive state at
  1094. * wake up.
  1095. */
  1096. static int omap2_mcspi_resume(struct device *dev)
  1097. {
  1098. struct spi_master *master = dev_get_drvdata(dev);
  1099. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1100. struct omap2_mcspi_cs *cs;
  1101. omap2_mcspi_enable_clocks(mcspi);
  1102. list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
  1103. node) {
  1104. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1105. /*
  1106. * We need to toggle CS state for OMAP take this
  1107. * change in account.
  1108. */
  1109. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
  1110. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1111. MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
  1112. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  1113. }
  1114. }
  1115. omap2_mcspi_disable_clocks(mcspi);
  1116. return 0;
  1117. }
  1118. #else
  1119. #define omap2_mcspi_resume NULL
  1120. #endif
  1121. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1122. .resume = omap2_mcspi_resume,
  1123. };
  1124. static struct platform_driver omap2_mcspi_driver = {
  1125. .driver = {
  1126. .name = "omap2_mcspi",
  1127. .owner = THIS_MODULE,
  1128. .pm = &omap2_mcspi_pm_ops
  1129. },
  1130. .remove = __exit_p(omap2_mcspi_remove),
  1131. };
  1132. static int __init omap2_mcspi_init(void)
  1133. {
  1134. omap2_mcspi_wq = create_singlethread_workqueue(
  1135. omap2_mcspi_driver.driver.name);
  1136. if (omap2_mcspi_wq == NULL)
  1137. return -1;
  1138. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1139. }
  1140. subsys_initcall(omap2_mcspi_init);
  1141. static void __exit omap2_mcspi_exit(void)
  1142. {
  1143. platform_driver_unregister(&omap2_mcspi_driver);
  1144. destroy_workqueue(omap2_mcspi_wq);
  1145. }
  1146. module_exit(omap2_mcspi_exit);
  1147. MODULE_LICENSE("GPL");