spi-tegra20-slink.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kthread.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/spi/spi.h>
  36. #include <linux/spi/spi-tegra.h>
  37. #include <linux/clk/tegra.h>
  38. #define SLINK_COMMAND 0x000
  39. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  40. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  41. #define SLINK_BOTH_EN (1 << 10)
  42. #define SLINK_CS_SW (1 << 11)
  43. #define SLINK_CS_VALUE (1 << 12)
  44. #define SLINK_CS_POLARITY (1 << 13)
  45. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  46. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  47. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  48. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  49. #define SLINK_IDLE_SDA_MASK (3 << 16)
  50. #define SLINK_CS_POLARITY1 (1 << 20)
  51. #define SLINK_CK_SDA (1 << 21)
  52. #define SLINK_CS_POLARITY2 (1 << 22)
  53. #define SLINK_CS_POLARITY3 (1 << 23)
  54. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  55. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  56. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  57. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  58. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  59. #define SLINK_M_S (1 << 28)
  60. #define SLINK_WAIT (1 << 29)
  61. #define SLINK_GO (1 << 30)
  62. #define SLINK_ENB (1 << 31)
  63. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  64. #define SLINK_COMMAND2 0x004
  65. #define SLINK_LSBFE (1 << 0)
  66. #define SLINK_SSOE (1 << 1)
  67. #define SLINK_SPIE (1 << 4)
  68. #define SLINK_BIDIROE (1 << 6)
  69. #define SLINK_MODFEN (1 << 7)
  70. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  71. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  72. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  73. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  74. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  75. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  76. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  77. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  78. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  79. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  80. #define SLINK_SPC0 (1 << 29)
  81. #define SLINK_TXEN (1 << 30)
  82. #define SLINK_RXEN (1 << 31)
  83. #define SLINK_STATUS 0x008
  84. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  85. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  86. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  87. #define SLINK_MODF (1 << 16)
  88. #define SLINK_RX_UNF (1 << 18)
  89. #define SLINK_TX_OVF (1 << 19)
  90. #define SLINK_TX_FULL (1 << 20)
  91. #define SLINK_TX_EMPTY (1 << 21)
  92. #define SLINK_RX_FULL (1 << 22)
  93. #define SLINK_RX_EMPTY (1 << 23)
  94. #define SLINK_TX_UNF (1 << 24)
  95. #define SLINK_RX_OVF (1 << 25)
  96. #define SLINK_TX_FLUSH (1 << 26)
  97. #define SLINK_RX_FLUSH (1 << 27)
  98. #define SLINK_SCLK (1 << 28)
  99. #define SLINK_ERR (1 << 29)
  100. #define SLINK_RDY (1 << 30)
  101. #define SLINK_BSY (1 << 31)
  102. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  103. SLINK_TX_UNF | SLINK_RX_OVF)
  104. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  105. #define SLINK_MAS_DATA 0x010
  106. #define SLINK_SLAVE_DATA 0x014
  107. #define SLINK_DMA_CTL 0x018
  108. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  109. #define SLINK_TX_TRIG_1 (0 << 16)
  110. #define SLINK_TX_TRIG_4 (1 << 16)
  111. #define SLINK_TX_TRIG_8 (2 << 16)
  112. #define SLINK_TX_TRIG_16 (3 << 16)
  113. #define SLINK_TX_TRIG_MASK (3 << 16)
  114. #define SLINK_RX_TRIG_1 (0 << 18)
  115. #define SLINK_RX_TRIG_4 (1 << 18)
  116. #define SLINK_RX_TRIG_8 (2 << 18)
  117. #define SLINK_RX_TRIG_16 (3 << 18)
  118. #define SLINK_RX_TRIG_MASK (3 << 18)
  119. #define SLINK_PACKED (1 << 20)
  120. #define SLINK_PACK_SIZE_4 (0 << 21)
  121. #define SLINK_PACK_SIZE_8 (1 << 21)
  122. #define SLINK_PACK_SIZE_16 (2 << 21)
  123. #define SLINK_PACK_SIZE_32 (3 << 21)
  124. #define SLINK_PACK_SIZE_MASK (3 << 21)
  125. #define SLINK_IE_TXC (1 << 26)
  126. #define SLINK_IE_RXC (1 << 27)
  127. #define SLINK_DMA_EN (1 << 31)
  128. #define SLINK_STATUS2 0x01c
  129. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  130. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  131. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  132. #define SLINK_TX_FIFO 0x100
  133. #define SLINK_RX_FIFO 0x180
  134. #define DATA_DIR_TX (1 << 0)
  135. #define DATA_DIR_RX (1 << 1)
  136. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  137. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  138. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  139. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  140. #define SLINK_STATUS2_RESET \
  141. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  142. #define MAX_CHIP_SELECT 4
  143. #define SLINK_FIFO_DEPTH 32
  144. struct tegra_slink_chip_data {
  145. bool cs_hold_time;
  146. };
  147. struct tegra_slink_data {
  148. struct device *dev;
  149. struct spi_master *master;
  150. const struct tegra_slink_chip_data *chip_data;
  151. spinlock_t lock;
  152. struct clk *clk;
  153. void __iomem *base;
  154. phys_addr_t phys;
  155. unsigned irq;
  156. int dma_req_sel;
  157. u32 spi_max_frequency;
  158. u32 cur_speed;
  159. struct spi_device *cur_spi;
  160. unsigned cur_pos;
  161. unsigned cur_len;
  162. unsigned words_per_32bit;
  163. unsigned bytes_per_word;
  164. unsigned curr_dma_words;
  165. unsigned cur_direction;
  166. unsigned cur_rx_pos;
  167. unsigned cur_tx_pos;
  168. unsigned dma_buf_size;
  169. unsigned max_buf_size;
  170. bool is_curr_dma_xfer;
  171. bool is_hw_based_cs;
  172. struct completion rx_dma_complete;
  173. struct completion tx_dma_complete;
  174. u32 tx_status;
  175. u32 rx_status;
  176. u32 status_reg;
  177. bool is_packed;
  178. unsigned long packed_size;
  179. u32 command_reg;
  180. u32 command2_reg;
  181. u32 dma_control_reg;
  182. u32 def_command_reg;
  183. u32 def_command2_reg;
  184. struct completion xfer_completion;
  185. struct spi_transfer *curr_xfer;
  186. struct dma_chan *rx_dma_chan;
  187. u32 *rx_dma_buf;
  188. dma_addr_t rx_dma_phys;
  189. struct dma_async_tx_descriptor *rx_dma_desc;
  190. struct dma_chan *tx_dma_chan;
  191. u32 *tx_dma_buf;
  192. dma_addr_t tx_dma_phys;
  193. struct dma_async_tx_descriptor *tx_dma_desc;
  194. };
  195. static int tegra_slink_runtime_suspend(struct device *dev);
  196. static int tegra_slink_runtime_resume(struct device *dev);
  197. static inline unsigned long tegra_slink_readl(struct tegra_slink_data *tspi,
  198. unsigned long reg)
  199. {
  200. return readl(tspi->base + reg);
  201. }
  202. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  203. unsigned long val, unsigned long reg)
  204. {
  205. writel(val, tspi->base + reg);
  206. /* Read back register to make sure that register writes completed */
  207. if (reg != SLINK_TX_FIFO)
  208. readl(tspi->base + SLINK_MAS_DATA);
  209. }
  210. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  211. {
  212. unsigned long val;
  213. unsigned long val_write = 0;
  214. val = tegra_slink_readl(tspi, SLINK_STATUS);
  215. /* Write 1 to clear status register */
  216. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  217. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  218. }
  219. static unsigned long tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  220. struct spi_transfer *t)
  221. {
  222. unsigned long val;
  223. switch (tspi->bytes_per_word) {
  224. case 0:
  225. val = SLINK_PACK_SIZE_4;
  226. break;
  227. case 1:
  228. val = SLINK_PACK_SIZE_8;
  229. break;
  230. case 2:
  231. val = SLINK_PACK_SIZE_16;
  232. break;
  233. case 4:
  234. val = SLINK_PACK_SIZE_32;
  235. break;
  236. default:
  237. val = 0;
  238. }
  239. return val;
  240. }
  241. static unsigned tegra_slink_calculate_curr_xfer_param(
  242. struct spi_device *spi, struct tegra_slink_data *tspi,
  243. struct spi_transfer *t)
  244. {
  245. unsigned remain_len = t->len - tspi->cur_pos;
  246. unsigned max_word;
  247. unsigned bits_per_word ;
  248. unsigned max_len;
  249. unsigned total_fifo_words;
  250. bits_per_word = t->bits_per_word;
  251. tspi->bytes_per_word = (bits_per_word - 1) / 8 + 1;
  252. if (bits_per_word == 8 || bits_per_word == 16) {
  253. tspi->is_packed = 1;
  254. tspi->words_per_32bit = 32/bits_per_word;
  255. } else {
  256. tspi->is_packed = 0;
  257. tspi->words_per_32bit = 1;
  258. }
  259. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  260. if (tspi->is_packed) {
  261. max_len = min(remain_len, tspi->max_buf_size);
  262. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  263. total_fifo_words = max_len/4;
  264. } else {
  265. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  266. max_word = min(max_word, tspi->max_buf_size/4);
  267. tspi->curr_dma_words = max_word;
  268. total_fifo_words = max_word;
  269. }
  270. return total_fifo_words;
  271. }
  272. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  273. struct tegra_slink_data *tspi, struct spi_transfer *t)
  274. {
  275. unsigned nbytes;
  276. unsigned tx_empty_count;
  277. unsigned long fifo_status;
  278. unsigned max_n_32bit;
  279. unsigned i, count;
  280. unsigned long x;
  281. unsigned int written_words;
  282. unsigned fifo_words_left;
  283. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  284. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  285. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  286. if (tspi->is_packed) {
  287. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  288. written_words = min(fifo_words_left, tspi->curr_dma_words);
  289. nbytes = written_words * tspi->bytes_per_word;
  290. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  291. for (count = 0; count < max_n_32bit; count++) {
  292. x = 0;
  293. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  294. x |= (*tx_buf++) << (i*8);
  295. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  296. }
  297. } else {
  298. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  299. written_words = max_n_32bit;
  300. nbytes = written_words * tspi->bytes_per_word;
  301. for (count = 0; count < max_n_32bit; count++) {
  302. x = 0;
  303. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  304. i++, nbytes--)
  305. x |= ((*tx_buf++) << i*8);
  306. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  307. }
  308. }
  309. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  310. return written_words;
  311. }
  312. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  313. struct tegra_slink_data *tspi, struct spi_transfer *t)
  314. {
  315. unsigned rx_full_count;
  316. unsigned long fifo_status;
  317. unsigned i, count;
  318. unsigned long x;
  319. unsigned int read_words = 0;
  320. unsigned len;
  321. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  322. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  323. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  324. if (tspi->is_packed) {
  325. len = tspi->curr_dma_words * tspi->bytes_per_word;
  326. for (count = 0; count < rx_full_count; count++) {
  327. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  328. for (i = 0; len && (i < 4); i++, len--)
  329. *rx_buf++ = (x >> i*8) & 0xFF;
  330. }
  331. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  332. read_words += tspi->curr_dma_words;
  333. } else {
  334. unsigned int bits_per_word;
  335. bits_per_word = t->bits_per_word;
  336. for (count = 0; count < rx_full_count; count++) {
  337. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  338. for (i = 0; (i < tspi->bytes_per_word); i++)
  339. *rx_buf++ = (x >> (i*8)) & 0xFF;
  340. }
  341. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  342. read_words += rx_full_count;
  343. }
  344. return read_words;
  345. }
  346. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  347. struct tegra_slink_data *tspi, struct spi_transfer *t)
  348. {
  349. unsigned len;
  350. /* Make the dma buffer to read by cpu */
  351. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  352. tspi->dma_buf_size, DMA_TO_DEVICE);
  353. if (tspi->is_packed) {
  354. len = tspi->curr_dma_words * tspi->bytes_per_word;
  355. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  356. } else {
  357. unsigned int i;
  358. unsigned int count;
  359. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  360. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  361. unsigned int x;
  362. for (count = 0; count < tspi->curr_dma_words; count++) {
  363. x = 0;
  364. for (i = 0; consume && (i < tspi->bytes_per_word);
  365. i++, consume--)
  366. x |= ((*tx_buf++) << i * 8);
  367. tspi->tx_dma_buf[count] = x;
  368. }
  369. }
  370. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  371. /* Make the dma buffer to read by dma */
  372. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  373. tspi->dma_buf_size, DMA_TO_DEVICE);
  374. }
  375. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  376. struct tegra_slink_data *tspi, struct spi_transfer *t)
  377. {
  378. unsigned len;
  379. /* Make the dma buffer to read by cpu */
  380. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  381. tspi->dma_buf_size, DMA_FROM_DEVICE);
  382. if (tspi->is_packed) {
  383. len = tspi->curr_dma_words * tspi->bytes_per_word;
  384. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  385. } else {
  386. unsigned int i;
  387. unsigned int count;
  388. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  389. unsigned int x;
  390. unsigned int rx_mask, bits_per_word;
  391. bits_per_word = t->bits_per_word;
  392. rx_mask = (1 << bits_per_word) - 1;
  393. for (count = 0; count < tspi->curr_dma_words; count++) {
  394. x = tspi->rx_dma_buf[count];
  395. x &= rx_mask;
  396. for (i = 0; (i < tspi->bytes_per_word); i++)
  397. *rx_buf++ = (x >> (i*8)) & 0xFF;
  398. }
  399. }
  400. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  401. /* Make the dma buffer to read by dma */
  402. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  403. tspi->dma_buf_size, DMA_FROM_DEVICE);
  404. }
  405. static void tegra_slink_dma_complete(void *args)
  406. {
  407. struct completion *dma_complete = args;
  408. complete(dma_complete);
  409. }
  410. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  411. {
  412. INIT_COMPLETION(tspi->tx_dma_complete);
  413. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  414. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  415. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  416. if (!tspi->tx_dma_desc) {
  417. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  418. return -EIO;
  419. }
  420. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  421. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  422. dmaengine_submit(tspi->tx_dma_desc);
  423. dma_async_issue_pending(tspi->tx_dma_chan);
  424. return 0;
  425. }
  426. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  427. {
  428. INIT_COMPLETION(tspi->rx_dma_complete);
  429. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  430. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  431. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  432. if (!tspi->rx_dma_desc) {
  433. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  434. return -EIO;
  435. }
  436. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  437. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  438. dmaengine_submit(tspi->rx_dma_desc);
  439. dma_async_issue_pending(tspi->rx_dma_chan);
  440. return 0;
  441. }
  442. static int tegra_slink_start_dma_based_transfer(
  443. struct tegra_slink_data *tspi, struct spi_transfer *t)
  444. {
  445. unsigned long val;
  446. unsigned long test_val;
  447. unsigned int len;
  448. int ret = 0;
  449. unsigned long status;
  450. /* Make sure that Rx and Tx fifo are empty */
  451. status = tegra_slink_readl(tspi, SLINK_STATUS);
  452. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  453. dev_err(tspi->dev,
  454. "Rx/Tx fifo are not empty status 0x%08lx\n", status);
  455. return -EIO;
  456. }
  457. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  458. val |= tspi->packed_size;
  459. if (tspi->is_packed)
  460. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  461. 4) * 4;
  462. else
  463. len = tspi->curr_dma_words * 4;
  464. /* Set attention level based on length of transfer */
  465. if (len & 0xF)
  466. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  467. else if (((len) >> 4) & 0x1)
  468. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  469. else
  470. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  471. if (tspi->cur_direction & DATA_DIR_TX)
  472. val |= SLINK_IE_TXC;
  473. if (tspi->cur_direction & DATA_DIR_RX)
  474. val |= SLINK_IE_RXC;
  475. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  476. tspi->dma_control_reg = val;
  477. if (tspi->cur_direction & DATA_DIR_TX) {
  478. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  479. wmb();
  480. ret = tegra_slink_start_tx_dma(tspi, len);
  481. if (ret < 0) {
  482. dev_err(tspi->dev,
  483. "Starting tx dma failed, err %d\n", ret);
  484. return ret;
  485. }
  486. /* Wait for tx fifo to be fill before starting slink */
  487. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  488. while (!(test_val & SLINK_TX_FULL))
  489. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  490. }
  491. if (tspi->cur_direction & DATA_DIR_RX) {
  492. /* Make the dma buffer to read by dma */
  493. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  494. tspi->dma_buf_size, DMA_FROM_DEVICE);
  495. ret = tegra_slink_start_rx_dma(tspi, len);
  496. if (ret < 0) {
  497. dev_err(tspi->dev,
  498. "Starting rx dma failed, err %d\n", ret);
  499. if (tspi->cur_direction & DATA_DIR_TX)
  500. dmaengine_terminate_all(tspi->tx_dma_chan);
  501. return ret;
  502. }
  503. }
  504. tspi->is_curr_dma_xfer = true;
  505. if (tspi->is_packed) {
  506. val |= SLINK_PACKED;
  507. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  508. /* HW need small delay after settign Packed mode */
  509. udelay(1);
  510. }
  511. tspi->dma_control_reg = val;
  512. val |= SLINK_DMA_EN;
  513. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  514. return ret;
  515. }
  516. static int tegra_slink_start_cpu_based_transfer(
  517. struct tegra_slink_data *tspi, struct spi_transfer *t)
  518. {
  519. unsigned long val;
  520. unsigned cur_words;
  521. val = tspi->packed_size;
  522. if (tspi->cur_direction & DATA_DIR_TX)
  523. val |= SLINK_IE_TXC;
  524. if (tspi->cur_direction & DATA_DIR_RX)
  525. val |= SLINK_IE_RXC;
  526. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  527. tspi->dma_control_reg = val;
  528. if (tspi->cur_direction & DATA_DIR_TX)
  529. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  530. else
  531. cur_words = tspi->curr_dma_words;
  532. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  533. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  534. tspi->dma_control_reg = val;
  535. tspi->is_curr_dma_xfer = false;
  536. if (tspi->is_packed) {
  537. val |= SLINK_PACKED;
  538. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  539. udelay(1);
  540. wmb();
  541. }
  542. tspi->dma_control_reg = val;
  543. val |= SLINK_DMA_EN;
  544. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  545. return 0;
  546. }
  547. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  548. bool dma_to_memory)
  549. {
  550. struct dma_chan *dma_chan;
  551. u32 *dma_buf;
  552. dma_addr_t dma_phys;
  553. int ret;
  554. struct dma_slave_config dma_sconfig;
  555. dma_cap_mask_t mask;
  556. dma_cap_zero(mask);
  557. dma_cap_set(DMA_SLAVE, mask);
  558. dma_chan = dma_request_channel(mask, NULL, NULL);
  559. if (!dma_chan) {
  560. dev_err(tspi->dev,
  561. "Dma channel is not available, will try later\n");
  562. return -EPROBE_DEFER;
  563. }
  564. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  565. &dma_phys, GFP_KERNEL);
  566. if (!dma_buf) {
  567. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  568. dma_release_channel(dma_chan);
  569. return -ENOMEM;
  570. }
  571. dma_sconfig.slave_id = tspi->dma_req_sel;
  572. if (dma_to_memory) {
  573. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  574. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  575. dma_sconfig.src_maxburst = 0;
  576. } else {
  577. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  578. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  579. dma_sconfig.dst_maxburst = 0;
  580. }
  581. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  582. if (ret)
  583. goto scrub;
  584. if (dma_to_memory) {
  585. tspi->rx_dma_chan = dma_chan;
  586. tspi->rx_dma_buf = dma_buf;
  587. tspi->rx_dma_phys = dma_phys;
  588. } else {
  589. tspi->tx_dma_chan = dma_chan;
  590. tspi->tx_dma_buf = dma_buf;
  591. tspi->tx_dma_phys = dma_phys;
  592. }
  593. return 0;
  594. scrub:
  595. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  596. dma_release_channel(dma_chan);
  597. return ret;
  598. }
  599. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  600. bool dma_to_memory)
  601. {
  602. u32 *dma_buf;
  603. dma_addr_t dma_phys;
  604. struct dma_chan *dma_chan;
  605. if (dma_to_memory) {
  606. dma_buf = tspi->rx_dma_buf;
  607. dma_chan = tspi->rx_dma_chan;
  608. dma_phys = tspi->rx_dma_phys;
  609. tspi->rx_dma_chan = NULL;
  610. tspi->rx_dma_buf = NULL;
  611. } else {
  612. dma_buf = tspi->tx_dma_buf;
  613. dma_chan = tspi->tx_dma_chan;
  614. dma_phys = tspi->tx_dma_phys;
  615. tspi->tx_dma_buf = NULL;
  616. tspi->tx_dma_chan = NULL;
  617. }
  618. if (!dma_chan)
  619. return;
  620. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  621. dma_release_channel(dma_chan);
  622. }
  623. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  624. struct spi_transfer *t, bool is_first_of_msg,
  625. bool is_single_xfer)
  626. {
  627. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  628. u32 speed;
  629. u8 bits_per_word;
  630. unsigned total_fifo_words;
  631. int ret;
  632. struct tegra_spi_device_controller_data *cdata = spi->controller_data;
  633. unsigned long command;
  634. unsigned long command2;
  635. bits_per_word = t->bits_per_word;
  636. speed = t->speed_hz;
  637. if (speed != tspi->cur_speed) {
  638. clk_set_rate(tspi->clk, speed * 4);
  639. tspi->cur_speed = speed;
  640. }
  641. tspi->cur_spi = spi;
  642. tspi->cur_pos = 0;
  643. tspi->cur_rx_pos = 0;
  644. tspi->cur_tx_pos = 0;
  645. tspi->curr_xfer = t;
  646. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  647. if (is_first_of_msg) {
  648. tegra_slink_clear_status(tspi);
  649. command = tspi->def_command_reg;
  650. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  651. command2 = tspi->def_command2_reg;
  652. command2 |= SLINK_SS_EN_CS(spi->chip_select);
  653. /* possibly use the hw based chip select */
  654. tspi->is_hw_based_cs = false;
  655. if (cdata && cdata->is_hw_based_cs && is_single_xfer &&
  656. ((tspi->curr_dma_words * tspi->bytes_per_word) ==
  657. (t->len - tspi->cur_pos))) {
  658. int setup_count;
  659. int sts2;
  660. setup_count = cdata->cs_setup_clk_count >> 1;
  661. setup_count = max(setup_count, 3);
  662. command2 |= SLINK_SS_SETUP(setup_count);
  663. if (tspi->chip_data->cs_hold_time) {
  664. int hold_count;
  665. hold_count = cdata->cs_hold_clk_count;
  666. hold_count = max(hold_count, 0xF);
  667. sts2 = tegra_slink_readl(tspi, SLINK_STATUS2);
  668. sts2 &= ~SLINK_SS_HOLD_TIME(0xF);
  669. sts2 |= SLINK_SS_HOLD_TIME(hold_count);
  670. tegra_slink_writel(tspi, sts2, SLINK_STATUS2);
  671. }
  672. tspi->is_hw_based_cs = true;
  673. }
  674. if (tspi->is_hw_based_cs)
  675. command &= ~SLINK_CS_SW;
  676. else
  677. command |= SLINK_CS_SW | SLINK_CS_VALUE;
  678. command &= ~SLINK_MODES;
  679. if (spi->mode & SPI_CPHA)
  680. command |= SLINK_CK_SDA;
  681. if (spi->mode & SPI_CPOL)
  682. command |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  683. else
  684. command |= SLINK_IDLE_SCLK_DRIVE_LOW;
  685. } else {
  686. command = tspi->command_reg;
  687. command &= ~SLINK_BIT_LENGTH(~0);
  688. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  689. command2 = tspi->command2_reg;
  690. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  691. }
  692. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  693. tspi->command_reg = command;
  694. tspi->cur_direction = 0;
  695. if (t->rx_buf) {
  696. command2 |= SLINK_RXEN;
  697. tspi->cur_direction |= DATA_DIR_RX;
  698. }
  699. if (t->tx_buf) {
  700. command2 |= SLINK_TXEN;
  701. tspi->cur_direction |= DATA_DIR_TX;
  702. }
  703. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  704. tspi->command2_reg = command2;
  705. if (total_fifo_words > SLINK_FIFO_DEPTH)
  706. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  707. else
  708. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  709. return ret;
  710. }
  711. static int tegra_slink_setup(struct spi_device *spi)
  712. {
  713. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  714. unsigned long val;
  715. unsigned long flags;
  716. int ret;
  717. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  718. SLINK_CS_POLARITY,
  719. SLINK_CS_POLARITY1,
  720. SLINK_CS_POLARITY2,
  721. SLINK_CS_POLARITY3,
  722. };
  723. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  724. spi->bits_per_word,
  725. spi->mode & SPI_CPOL ? "" : "~",
  726. spi->mode & SPI_CPHA ? "" : "~",
  727. spi->max_speed_hz);
  728. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  729. /* Set speed to the spi max fequency if spi device has not set */
  730. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  731. ret = pm_runtime_get_sync(tspi->dev);
  732. if (ret < 0) {
  733. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  734. return ret;
  735. }
  736. spin_lock_irqsave(&tspi->lock, flags);
  737. val = tspi->def_command_reg;
  738. if (spi->mode & SPI_CS_HIGH)
  739. val |= cs_pol_bit[spi->chip_select];
  740. else
  741. val &= ~cs_pol_bit[spi->chip_select];
  742. tspi->def_command_reg = val;
  743. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  744. spin_unlock_irqrestore(&tspi->lock, flags);
  745. pm_runtime_put(tspi->dev);
  746. return 0;
  747. }
  748. static int tegra_slink_prepare_transfer(struct spi_master *master)
  749. {
  750. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  751. return pm_runtime_get_sync(tspi->dev);
  752. }
  753. static int tegra_slink_unprepare_transfer(struct spi_master *master)
  754. {
  755. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  756. pm_runtime_put(tspi->dev);
  757. return 0;
  758. }
  759. static int tegra_slink_transfer_one_message(struct spi_master *master,
  760. struct spi_message *msg)
  761. {
  762. bool is_first_msg = true;
  763. int single_xfer;
  764. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  765. struct spi_transfer *xfer;
  766. struct spi_device *spi = msg->spi;
  767. int ret;
  768. msg->status = 0;
  769. msg->actual_length = 0;
  770. single_xfer = list_is_singular(&msg->transfers);
  771. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  772. INIT_COMPLETION(tspi->xfer_completion);
  773. ret = tegra_slink_start_transfer_one(spi, xfer,
  774. is_first_msg, single_xfer);
  775. if (ret < 0) {
  776. dev_err(tspi->dev,
  777. "spi can not start transfer, err %d\n", ret);
  778. goto exit;
  779. }
  780. is_first_msg = false;
  781. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  782. SLINK_DMA_TIMEOUT);
  783. if (WARN_ON(ret == 0)) {
  784. dev_err(tspi->dev,
  785. "spi trasfer timeout, err %d\n", ret);
  786. ret = -EIO;
  787. goto exit;
  788. }
  789. if (tspi->tx_status || tspi->rx_status) {
  790. dev_err(tspi->dev, "Error in Transfer\n");
  791. ret = -EIO;
  792. goto exit;
  793. }
  794. msg->actual_length += xfer->len;
  795. if (xfer->cs_change && xfer->delay_usecs) {
  796. tegra_slink_writel(tspi, tspi->def_command_reg,
  797. SLINK_COMMAND);
  798. udelay(xfer->delay_usecs);
  799. }
  800. }
  801. ret = 0;
  802. exit:
  803. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  804. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  805. msg->status = ret;
  806. spi_finalize_current_message(master);
  807. return ret;
  808. }
  809. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  810. {
  811. struct spi_transfer *t = tspi->curr_xfer;
  812. unsigned long flags;
  813. spin_lock_irqsave(&tspi->lock, flags);
  814. if (tspi->tx_status || tspi->rx_status ||
  815. (tspi->status_reg & SLINK_BSY)) {
  816. dev_err(tspi->dev,
  817. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  818. dev_err(tspi->dev,
  819. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  820. tspi->command2_reg, tspi->dma_control_reg);
  821. tegra_periph_reset_assert(tspi->clk);
  822. udelay(2);
  823. tegra_periph_reset_deassert(tspi->clk);
  824. complete(&tspi->xfer_completion);
  825. goto exit;
  826. }
  827. if (tspi->cur_direction & DATA_DIR_RX)
  828. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  829. if (tspi->cur_direction & DATA_DIR_TX)
  830. tspi->cur_pos = tspi->cur_tx_pos;
  831. else
  832. tspi->cur_pos = tspi->cur_rx_pos;
  833. if (tspi->cur_pos == t->len) {
  834. complete(&tspi->xfer_completion);
  835. goto exit;
  836. }
  837. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  838. tegra_slink_start_cpu_based_transfer(tspi, t);
  839. exit:
  840. spin_unlock_irqrestore(&tspi->lock, flags);
  841. return IRQ_HANDLED;
  842. }
  843. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  844. {
  845. struct spi_transfer *t = tspi->curr_xfer;
  846. long wait_status;
  847. int err = 0;
  848. unsigned total_fifo_words;
  849. unsigned long flags;
  850. /* Abort dmas if any error */
  851. if (tspi->cur_direction & DATA_DIR_TX) {
  852. if (tspi->tx_status) {
  853. dmaengine_terminate_all(tspi->tx_dma_chan);
  854. err += 1;
  855. } else {
  856. wait_status = wait_for_completion_interruptible_timeout(
  857. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  858. if (wait_status <= 0) {
  859. dmaengine_terminate_all(tspi->tx_dma_chan);
  860. dev_err(tspi->dev, "TxDma Xfer failed\n");
  861. err += 1;
  862. }
  863. }
  864. }
  865. if (tspi->cur_direction & DATA_DIR_RX) {
  866. if (tspi->rx_status) {
  867. dmaengine_terminate_all(tspi->rx_dma_chan);
  868. err += 2;
  869. } else {
  870. wait_status = wait_for_completion_interruptible_timeout(
  871. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  872. if (wait_status <= 0) {
  873. dmaengine_terminate_all(tspi->rx_dma_chan);
  874. dev_err(tspi->dev, "RxDma Xfer failed\n");
  875. err += 2;
  876. }
  877. }
  878. }
  879. spin_lock_irqsave(&tspi->lock, flags);
  880. if (err) {
  881. dev_err(tspi->dev,
  882. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  883. dev_err(tspi->dev,
  884. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  885. tspi->command2_reg, tspi->dma_control_reg);
  886. tegra_periph_reset_assert(tspi->clk);
  887. udelay(2);
  888. tegra_periph_reset_deassert(tspi->clk);
  889. complete(&tspi->xfer_completion);
  890. spin_unlock_irqrestore(&tspi->lock, flags);
  891. return IRQ_HANDLED;
  892. }
  893. if (tspi->cur_direction & DATA_DIR_RX)
  894. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  895. if (tspi->cur_direction & DATA_DIR_TX)
  896. tspi->cur_pos = tspi->cur_tx_pos;
  897. else
  898. tspi->cur_pos = tspi->cur_rx_pos;
  899. if (tspi->cur_pos == t->len) {
  900. complete(&tspi->xfer_completion);
  901. goto exit;
  902. }
  903. /* Continue transfer in current message */
  904. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  905. tspi, t);
  906. if (total_fifo_words > SLINK_FIFO_DEPTH)
  907. err = tegra_slink_start_dma_based_transfer(tspi, t);
  908. else
  909. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  910. exit:
  911. spin_unlock_irqrestore(&tspi->lock, flags);
  912. return IRQ_HANDLED;
  913. }
  914. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  915. {
  916. struct tegra_slink_data *tspi = context_data;
  917. if (!tspi->is_curr_dma_xfer)
  918. return handle_cpu_based_xfer(tspi);
  919. return handle_dma_based_xfer(tspi);
  920. }
  921. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  922. {
  923. struct tegra_slink_data *tspi = context_data;
  924. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  925. if (tspi->cur_direction & DATA_DIR_TX)
  926. tspi->tx_status = tspi->status_reg &
  927. (SLINK_TX_OVF | SLINK_TX_UNF);
  928. if (tspi->cur_direction & DATA_DIR_RX)
  929. tspi->rx_status = tspi->status_reg &
  930. (SLINK_RX_OVF | SLINK_RX_UNF);
  931. tegra_slink_clear_status(tspi);
  932. return IRQ_WAKE_THREAD;
  933. }
  934. static struct tegra_spi_platform_data *tegra_slink_parse_dt(
  935. struct platform_device *pdev)
  936. {
  937. struct tegra_spi_platform_data *pdata;
  938. const unsigned int *prop;
  939. struct device_node *np = pdev->dev.of_node;
  940. u32 of_dma[2];
  941. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  942. if (!pdata) {
  943. dev_err(&pdev->dev, "Memory alloc for pdata failed\n");
  944. return NULL;
  945. }
  946. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  947. of_dma, 2) >= 0)
  948. pdata->dma_req_sel = of_dma[1];
  949. prop = of_get_property(np, "spi-max-frequency", NULL);
  950. if (prop)
  951. pdata->spi_max_frequency = be32_to_cpup(prop);
  952. return pdata;
  953. }
  954. const struct tegra_slink_chip_data tegra30_spi_cdata = {
  955. .cs_hold_time = true,
  956. };
  957. const struct tegra_slink_chip_data tegra20_spi_cdata = {
  958. .cs_hold_time = false,
  959. };
  960. static struct of_device_id tegra_slink_of_match[] = {
  961. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  962. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  963. {}
  964. };
  965. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  966. static int tegra_slink_probe(struct platform_device *pdev)
  967. {
  968. struct spi_master *master;
  969. struct tegra_slink_data *tspi;
  970. struct resource *r;
  971. struct tegra_spi_platform_data *pdata = pdev->dev.platform_data;
  972. int ret, spi_irq;
  973. const struct tegra_slink_chip_data *cdata = NULL;
  974. const struct of_device_id *match;
  975. match = of_match_device(of_match_ptr(tegra_slink_of_match), &pdev->dev);
  976. if (!match) {
  977. dev_err(&pdev->dev, "Error: No device match found\n");
  978. return -ENODEV;
  979. }
  980. cdata = match->data;
  981. if (!pdata && pdev->dev.of_node)
  982. pdata = tegra_slink_parse_dt(pdev);
  983. if (!pdata) {
  984. dev_err(&pdev->dev, "No platform data, exiting\n");
  985. return -ENODEV;
  986. }
  987. if (!pdata->spi_max_frequency)
  988. pdata->spi_max_frequency = 25000000; /* 25MHz */
  989. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  990. if (!master) {
  991. dev_err(&pdev->dev, "master allocation failed\n");
  992. return -ENOMEM;
  993. }
  994. /* the spi->mode bits understood by this driver: */
  995. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  996. master->setup = tegra_slink_setup;
  997. master->prepare_transfer_hardware = tegra_slink_prepare_transfer;
  998. master->transfer_one_message = tegra_slink_transfer_one_message;
  999. master->unprepare_transfer_hardware = tegra_slink_unprepare_transfer;
  1000. master->num_chipselect = MAX_CHIP_SELECT;
  1001. master->bus_num = -1;
  1002. dev_set_drvdata(&pdev->dev, master);
  1003. tspi = spi_master_get_devdata(master);
  1004. tspi->master = master;
  1005. tspi->dma_req_sel = pdata->dma_req_sel;
  1006. tspi->dev = &pdev->dev;
  1007. tspi->chip_data = cdata;
  1008. spin_lock_init(&tspi->lock);
  1009. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1010. if (!r) {
  1011. dev_err(&pdev->dev, "No IO memory resource\n");
  1012. ret = -ENODEV;
  1013. goto exit_free_master;
  1014. }
  1015. tspi->phys = r->start;
  1016. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  1017. if (IS_ERR(tspi->base)) {
  1018. ret = PTR_ERR(tspi->base);
  1019. goto exit_free_master;
  1020. }
  1021. spi_irq = platform_get_irq(pdev, 0);
  1022. tspi->irq = spi_irq;
  1023. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  1024. tegra_slink_isr_thread, IRQF_ONESHOT,
  1025. dev_name(&pdev->dev), tspi);
  1026. if (ret < 0) {
  1027. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  1028. tspi->irq);
  1029. goto exit_free_master;
  1030. }
  1031. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  1032. if (IS_ERR(tspi->clk)) {
  1033. dev_err(&pdev->dev, "can not get clock\n");
  1034. ret = PTR_ERR(tspi->clk);
  1035. goto exit_free_irq;
  1036. }
  1037. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  1038. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  1039. tspi->spi_max_frequency = pdata->spi_max_frequency;
  1040. if (pdata->dma_req_sel) {
  1041. ret = tegra_slink_init_dma_param(tspi, true);
  1042. if (ret < 0) {
  1043. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  1044. goto exit_free_irq;
  1045. }
  1046. ret = tegra_slink_init_dma_param(tspi, false);
  1047. if (ret < 0) {
  1048. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  1049. goto exit_rx_dma_free;
  1050. }
  1051. tspi->max_buf_size = tspi->dma_buf_size;
  1052. init_completion(&tspi->tx_dma_complete);
  1053. init_completion(&tspi->rx_dma_complete);
  1054. }
  1055. init_completion(&tspi->xfer_completion);
  1056. pm_runtime_enable(&pdev->dev);
  1057. if (!pm_runtime_enabled(&pdev->dev)) {
  1058. ret = tegra_slink_runtime_resume(&pdev->dev);
  1059. if (ret)
  1060. goto exit_pm_disable;
  1061. }
  1062. ret = pm_runtime_get_sync(&pdev->dev);
  1063. if (ret < 0) {
  1064. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  1065. goto exit_pm_disable;
  1066. }
  1067. tspi->def_command_reg = SLINK_M_S;
  1068. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  1069. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  1070. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  1071. pm_runtime_put(&pdev->dev);
  1072. master->dev.of_node = pdev->dev.of_node;
  1073. ret = spi_register_master(master);
  1074. if (ret < 0) {
  1075. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1076. goto exit_pm_disable;
  1077. }
  1078. return ret;
  1079. exit_pm_disable:
  1080. pm_runtime_disable(&pdev->dev);
  1081. if (!pm_runtime_status_suspended(&pdev->dev))
  1082. tegra_slink_runtime_suspend(&pdev->dev);
  1083. tegra_slink_deinit_dma_param(tspi, false);
  1084. exit_rx_dma_free:
  1085. tegra_slink_deinit_dma_param(tspi, true);
  1086. exit_free_irq:
  1087. free_irq(spi_irq, tspi);
  1088. exit_free_master:
  1089. spi_master_put(master);
  1090. return ret;
  1091. }
  1092. static int tegra_slink_remove(struct platform_device *pdev)
  1093. {
  1094. struct spi_master *master = dev_get_drvdata(&pdev->dev);
  1095. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1096. free_irq(tspi->irq, tspi);
  1097. spi_unregister_master(master);
  1098. if (tspi->tx_dma_chan)
  1099. tegra_slink_deinit_dma_param(tspi, false);
  1100. if (tspi->rx_dma_chan)
  1101. tegra_slink_deinit_dma_param(tspi, true);
  1102. pm_runtime_disable(&pdev->dev);
  1103. if (!pm_runtime_status_suspended(&pdev->dev))
  1104. tegra_slink_runtime_suspend(&pdev->dev);
  1105. return 0;
  1106. }
  1107. #ifdef CONFIG_PM_SLEEP
  1108. static int tegra_slink_suspend(struct device *dev)
  1109. {
  1110. struct spi_master *master = dev_get_drvdata(dev);
  1111. return spi_master_suspend(master);
  1112. }
  1113. static int tegra_slink_resume(struct device *dev)
  1114. {
  1115. struct spi_master *master = dev_get_drvdata(dev);
  1116. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1117. int ret;
  1118. ret = pm_runtime_get_sync(dev);
  1119. if (ret < 0) {
  1120. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1121. return ret;
  1122. }
  1123. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1124. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1125. pm_runtime_put(dev);
  1126. return spi_master_resume(master);
  1127. }
  1128. #endif
  1129. static int tegra_slink_runtime_suspend(struct device *dev)
  1130. {
  1131. struct spi_master *master = dev_get_drvdata(dev);
  1132. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1133. /* Flush all write which are in PPSB queue by reading back */
  1134. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1135. clk_disable_unprepare(tspi->clk);
  1136. return 0;
  1137. }
  1138. static int tegra_slink_runtime_resume(struct device *dev)
  1139. {
  1140. struct spi_master *master = dev_get_drvdata(dev);
  1141. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1142. int ret;
  1143. ret = clk_prepare_enable(tspi->clk);
  1144. if (ret < 0) {
  1145. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1146. return ret;
  1147. }
  1148. return 0;
  1149. }
  1150. static const struct dev_pm_ops slink_pm_ops = {
  1151. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1152. tegra_slink_runtime_resume, NULL)
  1153. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1154. };
  1155. static struct platform_driver tegra_slink_driver = {
  1156. .driver = {
  1157. .name = "spi-tegra-slink",
  1158. .owner = THIS_MODULE,
  1159. .pm = &slink_pm_ops,
  1160. .of_match_table = of_match_ptr(tegra_slink_of_match),
  1161. },
  1162. .probe = tegra_slink_probe,
  1163. .remove = tegra_slink_remove,
  1164. };
  1165. module_platform_driver(tegra_slink_driver);
  1166. MODULE_ALIAS("platform:spi-tegra-slink");
  1167. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1168. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1169. MODULE_LICENSE("GPL v2");