spi-pxa2xx.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/acpi.h>
  35. #include "spi-pxa2xx.h"
  36. MODULE_AUTHOR("Stephen Street");
  37. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("platform:pxa2xx-spi");
  40. #define TIMOUT_DFLT 1000
  41. /*
  42. * for testing SSCR1 changes that require SSP restart, basically
  43. * everything except the service and interrupt enables, the pxa270 developer
  44. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  45. * list, but the PXA255 dev man says all bits without really meaning the
  46. * service and interrupt enables
  47. */
  48. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  49. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  50. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  51. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  52. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  53. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  54. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  55. | QUARK_X1000_SSCR1_EFWR \
  56. | QUARK_X1000_SSCR1_RFT \
  57. | QUARK_X1000_SSCR1_TFT \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  60. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  61. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  62. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  63. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  64. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  65. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  66. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  67. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  68. #define LPSS_CAPS_CS_EN_SHIFT 9
  69. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  70. #define LPSS_PRIV_CLOCK_GATE 0x38
  71. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
  72. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
  73. struct lpss_config {
  74. /* LPSS offset from drv_data->ioaddr */
  75. unsigned offset;
  76. /* Register offsets from drv_data->lpss_base or -1 */
  77. int reg_general;
  78. int reg_ssp;
  79. int reg_cs_ctrl;
  80. int reg_capabilities;
  81. /* FIFO thresholds */
  82. u32 rx_threshold;
  83. u32 tx_threshold_lo;
  84. u32 tx_threshold_hi;
  85. /* Chip select control */
  86. unsigned cs_sel_shift;
  87. unsigned cs_sel_mask;
  88. unsigned cs_num;
  89. /* Quirks */
  90. unsigned cs_clk_stays_gated : 1;
  91. };
  92. /* Keep these sorted with enum pxa_ssp_type */
  93. static const struct lpss_config lpss_platforms[] = {
  94. { /* LPSS_LPT_SSP */
  95. .offset = 0x800,
  96. .reg_general = 0x08,
  97. .reg_ssp = 0x0c,
  98. .reg_cs_ctrl = 0x18,
  99. .reg_capabilities = -1,
  100. .rx_threshold = 64,
  101. .tx_threshold_lo = 160,
  102. .tx_threshold_hi = 224,
  103. },
  104. { /* LPSS_BYT_SSP */
  105. .offset = 0x400,
  106. .reg_general = 0x08,
  107. .reg_ssp = 0x0c,
  108. .reg_cs_ctrl = 0x18,
  109. .reg_capabilities = -1,
  110. .rx_threshold = 64,
  111. .tx_threshold_lo = 160,
  112. .tx_threshold_hi = 224,
  113. },
  114. { /* LPSS_BSW_SSP */
  115. .offset = 0x400,
  116. .reg_general = 0x08,
  117. .reg_ssp = 0x0c,
  118. .reg_cs_ctrl = 0x18,
  119. .reg_capabilities = -1,
  120. .rx_threshold = 64,
  121. .tx_threshold_lo = 160,
  122. .tx_threshold_hi = 224,
  123. .cs_sel_shift = 2,
  124. .cs_sel_mask = 1 << 2,
  125. .cs_num = 2,
  126. },
  127. { /* LPSS_SPT_SSP */
  128. .offset = 0x200,
  129. .reg_general = -1,
  130. .reg_ssp = 0x20,
  131. .reg_cs_ctrl = 0x24,
  132. .reg_capabilities = -1,
  133. .rx_threshold = 1,
  134. .tx_threshold_lo = 32,
  135. .tx_threshold_hi = 56,
  136. },
  137. { /* LPSS_BXT_SSP */
  138. .offset = 0x200,
  139. .reg_general = -1,
  140. .reg_ssp = 0x20,
  141. .reg_cs_ctrl = 0x24,
  142. .reg_capabilities = 0xfc,
  143. .rx_threshold = 1,
  144. .tx_threshold_lo = 16,
  145. .tx_threshold_hi = 48,
  146. .cs_sel_shift = 8,
  147. .cs_sel_mask = 3 << 8,
  148. .cs_clk_stays_gated = true,
  149. },
  150. { /* LPSS_CNL_SSP */
  151. .offset = 0x200,
  152. .reg_general = -1,
  153. .reg_ssp = 0x20,
  154. .reg_cs_ctrl = 0x24,
  155. .reg_capabilities = 0xfc,
  156. .rx_threshold = 1,
  157. .tx_threshold_lo = 32,
  158. .tx_threshold_hi = 56,
  159. .cs_sel_shift = 8,
  160. .cs_sel_mask = 3 << 8,
  161. .cs_clk_stays_gated = true,
  162. },
  163. };
  164. static inline const struct lpss_config
  165. *lpss_get_config(const struct driver_data *drv_data)
  166. {
  167. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  168. }
  169. static bool is_lpss_ssp(const struct driver_data *drv_data)
  170. {
  171. switch (drv_data->ssp_type) {
  172. case LPSS_LPT_SSP:
  173. case LPSS_BYT_SSP:
  174. case LPSS_BSW_SSP:
  175. case LPSS_SPT_SSP:
  176. case LPSS_BXT_SSP:
  177. case LPSS_CNL_SSP:
  178. return true;
  179. default:
  180. return false;
  181. }
  182. }
  183. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  184. {
  185. return drv_data->ssp_type == QUARK_X1000_SSP;
  186. }
  187. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  188. {
  189. switch (drv_data->ssp_type) {
  190. case QUARK_X1000_SSP:
  191. return QUARK_X1000_SSCR1_CHANGE_MASK;
  192. case CE4100_SSP:
  193. return CE4100_SSCR1_CHANGE_MASK;
  194. default:
  195. return SSCR1_CHANGE_MASK;
  196. }
  197. }
  198. static u32
  199. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  200. {
  201. switch (drv_data->ssp_type) {
  202. case QUARK_X1000_SSP:
  203. return RX_THRESH_QUARK_X1000_DFLT;
  204. case CE4100_SSP:
  205. return RX_THRESH_CE4100_DFLT;
  206. default:
  207. return RX_THRESH_DFLT;
  208. }
  209. }
  210. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  211. {
  212. u32 mask;
  213. switch (drv_data->ssp_type) {
  214. case QUARK_X1000_SSP:
  215. mask = QUARK_X1000_SSSR_TFL_MASK;
  216. break;
  217. case CE4100_SSP:
  218. mask = CE4100_SSSR_TFL_MASK;
  219. break;
  220. default:
  221. mask = SSSR_TFL_MASK;
  222. break;
  223. }
  224. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  225. }
  226. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  227. u32 *sccr1_reg)
  228. {
  229. u32 mask;
  230. switch (drv_data->ssp_type) {
  231. case QUARK_X1000_SSP:
  232. mask = QUARK_X1000_SSCR1_RFT;
  233. break;
  234. case CE4100_SSP:
  235. mask = CE4100_SSCR1_RFT;
  236. break;
  237. default:
  238. mask = SSCR1_RFT;
  239. break;
  240. }
  241. *sccr1_reg &= ~mask;
  242. }
  243. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  244. u32 *sccr1_reg, u32 threshold)
  245. {
  246. switch (drv_data->ssp_type) {
  247. case QUARK_X1000_SSP:
  248. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  249. break;
  250. case CE4100_SSP:
  251. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  252. break;
  253. default:
  254. *sccr1_reg |= SSCR1_RxTresh(threshold);
  255. break;
  256. }
  257. }
  258. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  259. u32 clk_div, u8 bits)
  260. {
  261. switch (drv_data->ssp_type) {
  262. case QUARK_X1000_SSP:
  263. return clk_div
  264. | QUARK_X1000_SSCR0_Motorola
  265. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  266. | SSCR0_SSE;
  267. default:
  268. return clk_div
  269. | SSCR0_Motorola
  270. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  271. | SSCR0_SSE
  272. | (bits > 16 ? SSCR0_EDSS : 0);
  273. }
  274. }
  275. /*
  276. * Read and write LPSS SSP private registers. Caller must first check that
  277. * is_lpss_ssp() returns true before these can be called.
  278. */
  279. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  280. {
  281. WARN_ON(!drv_data->lpss_base);
  282. return readl(drv_data->lpss_base + offset);
  283. }
  284. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  285. unsigned offset, u32 value)
  286. {
  287. WARN_ON(!drv_data->lpss_base);
  288. writel(value, drv_data->lpss_base + offset);
  289. }
  290. /*
  291. * lpss_ssp_setup - perform LPSS SSP specific setup
  292. * @drv_data: pointer to the driver private data
  293. *
  294. * Perform LPSS SSP specific setup. This function must be called first if
  295. * one is going to use LPSS SSP private registers.
  296. */
  297. static void lpss_ssp_setup(struct driver_data *drv_data)
  298. {
  299. const struct lpss_config *config;
  300. u32 value;
  301. config = lpss_get_config(drv_data);
  302. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  303. /* Enable software chip select control */
  304. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  305. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  306. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  307. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  308. /* Enable multiblock DMA transfers */
  309. if (drv_data->master_info->enable_dma) {
  310. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  311. if (config->reg_general >= 0) {
  312. value = __lpss_ssp_read_priv(drv_data,
  313. config->reg_general);
  314. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  315. __lpss_ssp_write_priv(drv_data,
  316. config->reg_general, value);
  317. }
  318. }
  319. }
  320. static void lpss_ssp_select_cs(struct driver_data *drv_data,
  321. const struct lpss_config *config)
  322. {
  323. u32 value, cs;
  324. if (!config->cs_sel_mask)
  325. return;
  326. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  327. cs = drv_data->master->cur_msg->spi->chip_select;
  328. cs <<= config->cs_sel_shift;
  329. if (cs != (value & config->cs_sel_mask)) {
  330. /*
  331. * When switching another chip select output active the
  332. * output must be selected first and wait 2 ssp_clk cycles
  333. * before changing state to active. Otherwise a short
  334. * glitch will occur on the previous chip select since
  335. * output select is latched but state control is not.
  336. */
  337. value &= ~config->cs_sel_mask;
  338. value |= cs;
  339. __lpss_ssp_write_priv(drv_data,
  340. config->reg_cs_ctrl, value);
  341. ndelay(1000000000 /
  342. (drv_data->master->max_speed_hz / 2));
  343. }
  344. }
  345. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  346. {
  347. const struct lpss_config *config;
  348. u32 value;
  349. config = lpss_get_config(drv_data);
  350. if (enable)
  351. lpss_ssp_select_cs(drv_data, config);
  352. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  353. if (enable)
  354. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  355. else
  356. value |= LPSS_CS_CONTROL_CS_HIGH;
  357. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  358. if (config->cs_clk_stays_gated) {
  359. u32 clkgate;
  360. /*
  361. * Changing CS alone when dynamic clock gating is on won't
  362. * actually flip CS at that time. This ruins SPI transfers
  363. * that specify delays, or have no data. Toggle the clock mode
  364. * to force on briefly to poke the CS pin to move.
  365. */
  366. clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
  367. value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
  368. LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
  369. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
  370. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
  371. }
  372. }
  373. static void cs_assert(struct driver_data *drv_data)
  374. {
  375. struct chip_data *chip =
  376. spi_get_ctldata(drv_data->master->cur_msg->spi);
  377. if (drv_data->ssp_type == CE4100_SSP) {
  378. pxa2xx_spi_write(drv_data, SSSR, chip->frm);
  379. return;
  380. }
  381. if (chip->cs_control) {
  382. chip->cs_control(PXA2XX_CS_ASSERT);
  383. return;
  384. }
  385. if (chip->gpiod_cs) {
  386. gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
  387. return;
  388. }
  389. if (is_lpss_ssp(drv_data))
  390. lpss_ssp_cs_control(drv_data, true);
  391. }
  392. static void cs_deassert(struct driver_data *drv_data)
  393. {
  394. struct chip_data *chip =
  395. spi_get_ctldata(drv_data->master->cur_msg->spi);
  396. if (drv_data->ssp_type == CE4100_SSP)
  397. return;
  398. if (chip->cs_control) {
  399. chip->cs_control(PXA2XX_CS_DEASSERT);
  400. return;
  401. }
  402. if (chip->gpiod_cs) {
  403. gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
  404. return;
  405. }
  406. if (is_lpss_ssp(drv_data))
  407. lpss_ssp_cs_control(drv_data, false);
  408. }
  409. int pxa2xx_spi_flush(struct driver_data *drv_data)
  410. {
  411. unsigned long limit = loops_per_jiffy << 1;
  412. do {
  413. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  414. pxa2xx_spi_read(drv_data, SSDR);
  415. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  416. write_SSSR_CS(drv_data, SSSR_ROR);
  417. return limit;
  418. }
  419. static int null_writer(struct driver_data *drv_data)
  420. {
  421. u8 n_bytes = drv_data->n_bytes;
  422. if (pxa2xx_spi_txfifo_full(drv_data)
  423. || (drv_data->tx == drv_data->tx_end))
  424. return 0;
  425. pxa2xx_spi_write(drv_data, SSDR, 0);
  426. drv_data->tx += n_bytes;
  427. return 1;
  428. }
  429. static int null_reader(struct driver_data *drv_data)
  430. {
  431. u8 n_bytes = drv_data->n_bytes;
  432. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  433. && (drv_data->rx < drv_data->rx_end)) {
  434. pxa2xx_spi_read(drv_data, SSDR);
  435. drv_data->rx += n_bytes;
  436. }
  437. return drv_data->rx == drv_data->rx_end;
  438. }
  439. static int u8_writer(struct driver_data *drv_data)
  440. {
  441. if (pxa2xx_spi_txfifo_full(drv_data)
  442. || (drv_data->tx == drv_data->tx_end))
  443. return 0;
  444. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  445. ++drv_data->tx;
  446. return 1;
  447. }
  448. static int u8_reader(struct driver_data *drv_data)
  449. {
  450. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  451. && (drv_data->rx < drv_data->rx_end)) {
  452. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  453. ++drv_data->rx;
  454. }
  455. return drv_data->rx == drv_data->rx_end;
  456. }
  457. static int u16_writer(struct driver_data *drv_data)
  458. {
  459. if (pxa2xx_spi_txfifo_full(drv_data)
  460. || (drv_data->tx == drv_data->tx_end))
  461. return 0;
  462. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  463. drv_data->tx += 2;
  464. return 1;
  465. }
  466. static int u16_reader(struct driver_data *drv_data)
  467. {
  468. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  469. && (drv_data->rx < drv_data->rx_end)) {
  470. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  471. drv_data->rx += 2;
  472. }
  473. return drv_data->rx == drv_data->rx_end;
  474. }
  475. static int u32_writer(struct driver_data *drv_data)
  476. {
  477. if (pxa2xx_spi_txfifo_full(drv_data)
  478. || (drv_data->tx == drv_data->tx_end))
  479. return 0;
  480. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  481. drv_data->tx += 4;
  482. return 1;
  483. }
  484. static int u32_reader(struct driver_data *drv_data)
  485. {
  486. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  487. && (drv_data->rx < drv_data->rx_end)) {
  488. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  489. drv_data->rx += 4;
  490. }
  491. return drv_data->rx == drv_data->rx_end;
  492. }
  493. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  494. {
  495. struct spi_message *msg = drv_data->master->cur_msg;
  496. struct spi_transfer *trans = drv_data->cur_transfer;
  497. /* Move to next transfer */
  498. if (trans->transfer_list.next != &msg->transfers) {
  499. drv_data->cur_transfer =
  500. list_entry(trans->transfer_list.next,
  501. struct spi_transfer,
  502. transfer_list);
  503. return RUNNING_STATE;
  504. } else
  505. return DONE_STATE;
  506. }
  507. /* caller already set message->status; dma and pio irqs are blocked */
  508. static void giveback(struct driver_data *drv_data)
  509. {
  510. struct spi_transfer* last_transfer;
  511. struct spi_message *msg;
  512. unsigned long timeout;
  513. msg = drv_data->master->cur_msg;
  514. drv_data->cur_transfer = NULL;
  515. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  516. transfer_list);
  517. /* Delay if requested before any change in chip select */
  518. if (last_transfer->delay_usecs)
  519. udelay(last_transfer->delay_usecs);
  520. /* Wait until SSP becomes idle before deasserting the CS */
  521. timeout = jiffies + msecs_to_jiffies(10);
  522. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  523. !time_after(jiffies, timeout))
  524. cpu_relax();
  525. /* Drop chip select UNLESS cs_change is true or we are returning
  526. * a message with an error, or next message is for another chip
  527. */
  528. if (!last_transfer->cs_change)
  529. cs_deassert(drv_data);
  530. else {
  531. struct spi_message *next_msg;
  532. /* Holding of cs was hinted, but we need to make sure
  533. * the next message is for the same chip. Don't waste
  534. * time with the following tests unless this was hinted.
  535. *
  536. * We cannot postpone this until pump_messages, because
  537. * after calling msg->complete (below) the driver that
  538. * sent the current message could be unloaded, which
  539. * could invalidate the cs_control() callback...
  540. */
  541. /* get a pointer to the next message, if any */
  542. next_msg = spi_get_next_queued_message(drv_data->master);
  543. /* see if the next and current messages point
  544. * to the same chip
  545. */
  546. if ((next_msg && next_msg->spi != msg->spi) ||
  547. msg->state == ERROR_STATE)
  548. cs_deassert(drv_data);
  549. }
  550. spi_finalize_current_message(drv_data->master);
  551. }
  552. static void reset_sccr1(struct driver_data *drv_data)
  553. {
  554. struct chip_data *chip =
  555. spi_get_ctldata(drv_data->master->cur_msg->spi);
  556. u32 sccr1_reg;
  557. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  558. switch (drv_data->ssp_type) {
  559. case QUARK_X1000_SSP:
  560. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  561. break;
  562. case CE4100_SSP:
  563. sccr1_reg &= ~CE4100_SSCR1_RFT;
  564. break;
  565. default:
  566. sccr1_reg &= ~SSCR1_RFT;
  567. break;
  568. }
  569. sccr1_reg |= chip->threshold;
  570. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  571. }
  572. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  573. {
  574. /* Stop and reset SSP */
  575. write_SSSR_CS(drv_data, drv_data->clear_sr);
  576. reset_sccr1(drv_data);
  577. if (!pxa25x_ssp_comp(drv_data))
  578. pxa2xx_spi_write(drv_data, SSTO, 0);
  579. pxa2xx_spi_flush(drv_data);
  580. pxa2xx_spi_write(drv_data, SSCR0,
  581. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  582. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  583. drv_data->master->cur_msg->state = ERROR_STATE;
  584. tasklet_schedule(&drv_data->pump_transfers);
  585. }
  586. static void int_transfer_complete(struct driver_data *drv_data)
  587. {
  588. /* Clear and disable interrupts */
  589. write_SSSR_CS(drv_data, drv_data->clear_sr);
  590. reset_sccr1(drv_data);
  591. if (!pxa25x_ssp_comp(drv_data))
  592. pxa2xx_spi_write(drv_data, SSTO, 0);
  593. /* Update total byte transferred return count actual bytes read */
  594. drv_data->master->cur_msg->actual_length += drv_data->len -
  595. (drv_data->rx_end - drv_data->rx);
  596. /* Transfer delays and chip select release are
  597. * handled in pump_transfers or giveback
  598. */
  599. /* Move to next transfer */
  600. drv_data->master->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  601. /* Schedule transfer tasklet */
  602. tasklet_schedule(&drv_data->pump_transfers);
  603. }
  604. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  605. {
  606. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  607. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  608. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  609. if (irq_status & SSSR_ROR) {
  610. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  611. return IRQ_HANDLED;
  612. }
  613. if (irq_status & SSSR_TINT) {
  614. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  615. if (drv_data->read(drv_data)) {
  616. int_transfer_complete(drv_data);
  617. return IRQ_HANDLED;
  618. }
  619. }
  620. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  621. do {
  622. if (drv_data->read(drv_data)) {
  623. int_transfer_complete(drv_data);
  624. return IRQ_HANDLED;
  625. }
  626. } while (drv_data->write(drv_data));
  627. if (drv_data->read(drv_data)) {
  628. int_transfer_complete(drv_data);
  629. return IRQ_HANDLED;
  630. }
  631. if (drv_data->tx == drv_data->tx_end) {
  632. u32 bytes_left;
  633. u32 sccr1_reg;
  634. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  635. sccr1_reg &= ~SSCR1_TIE;
  636. /*
  637. * PXA25x_SSP has no timeout, set up rx threshould for the
  638. * remaining RX bytes.
  639. */
  640. if (pxa25x_ssp_comp(drv_data)) {
  641. u32 rx_thre;
  642. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  643. bytes_left = drv_data->rx_end - drv_data->rx;
  644. switch (drv_data->n_bytes) {
  645. case 4:
  646. bytes_left >>= 1;
  647. case 2:
  648. bytes_left >>= 1;
  649. }
  650. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  651. if (rx_thre > bytes_left)
  652. rx_thre = bytes_left;
  653. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  654. }
  655. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  656. }
  657. /* We did something */
  658. return IRQ_HANDLED;
  659. }
  660. static void handle_bad_msg(struct driver_data *drv_data)
  661. {
  662. pxa2xx_spi_write(drv_data, SSCR0,
  663. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  664. pxa2xx_spi_write(drv_data, SSCR1,
  665. pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
  666. if (!pxa25x_ssp_comp(drv_data))
  667. pxa2xx_spi_write(drv_data, SSTO, 0);
  668. write_SSSR_CS(drv_data, drv_data->clear_sr);
  669. dev_err(&drv_data->pdev->dev,
  670. "bad message state in interrupt handler\n");
  671. }
  672. static irqreturn_t ssp_int(int irq, void *dev_id)
  673. {
  674. struct driver_data *drv_data = dev_id;
  675. u32 sccr1_reg;
  676. u32 mask = drv_data->mask_sr;
  677. u32 status;
  678. /*
  679. * The IRQ might be shared with other peripherals so we must first
  680. * check that are we RPM suspended or not. If we are we assume that
  681. * the IRQ was not for us (we shouldn't be RPM suspended when the
  682. * interrupt is enabled).
  683. */
  684. if (pm_runtime_suspended(&drv_data->pdev->dev))
  685. return IRQ_NONE;
  686. /*
  687. * If the device is not yet in RPM suspended state and we get an
  688. * interrupt that is meant for another device, check if status bits
  689. * are all set to one. That means that the device is already
  690. * powered off.
  691. */
  692. status = pxa2xx_spi_read(drv_data, SSSR);
  693. if (status == ~0)
  694. return IRQ_NONE;
  695. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  696. /* Ignore possible writes if we don't need to write */
  697. if (!(sccr1_reg & SSCR1_TIE))
  698. mask &= ~SSSR_TFS;
  699. /* Ignore RX timeout interrupt if it is disabled */
  700. if (!(sccr1_reg & SSCR1_TINTE))
  701. mask &= ~SSSR_TINT;
  702. if (!(status & mask))
  703. return IRQ_NONE;
  704. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
  705. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  706. if (!drv_data->master->cur_msg) {
  707. handle_bad_msg(drv_data);
  708. /* Never fail */
  709. return IRQ_HANDLED;
  710. }
  711. return drv_data->transfer_handler(drv_data);
  712. }
  713. /*
  714. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  715. * input frequency by fractions of 2^24. It also has a divider by 5.
  716. *
  717. * There are formulas to get baud rate value for given input frequency and
  718. * divider parameters, such as DDS_CLK_RATE and SCR:
  719. *
  720. * Fsys = 200MHz
  721. *
  722. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  723. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  724. *
  725. * DDS_CLK_RATE either 2^n or 2^n / 5.
  726. * SCR is in range 0 .. 255
  727. *
  728. * Divisor = 5^i * 2^j * 2 * k
  729. * i = [0, 1] i = 1 iff j = 0 or j > 3
  730. * j = [0, 23] j = 0 iff i = 1
  731. * k = [1, 256]
  732. * Special case: j = 0, i = 1: Divisor = 2 / 5
  733. *
  734. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  735. * are:
  736. * Case 1: 2^n, n = [0, 23]
  737. * Case 2: 2^24 * 2 / 5 (0x666666)
  738. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  739. *
  740. * In all cases the lowest possible value is better.
  741. *
  742. * The function calculates parameters for all cases and chooses the one closest
  743. * to the asked baud rate.
  744. */
  745. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  746. {
  747. unsigned long xtal = 200000000;
  748. unsigned long fref = xtal / 2; /* mandatory division by 2,
  749. see (2) */
  750. /* case 3 */
  751. unsigned long fref1 = fref / 2; /* case 1 */
  752. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  753. unsigned long scale;
  754. unsigned long q, q1, q2;
  755. long r, r1, r2;
  756. u32 mul;
  757. /* Case 1 */
  758. /* Set initial value for DDS_CLK_RATE */
  759. mul = (1 << 24) >> 1;
  760. /* Calculate initial quot */
  761. q1 = DIV_ROUND_UP(fref1, rate);
  762. /* Scale q1 if it's too big */
  763. if (q1 > 256) {
  764. /* Scale q1 to range [1, 512] */
  765. scale = fls_long(q1 - 1);
  766. if (scale > 9) {
  767. q1 >>= scale - 9;
  768. mul >>= scale - 9;
  769. }
  770. /* Round the result if we have a remainder */
  771. q1 += q1 & 1;
  772. }
  773. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  774. scale = __ffs(q1);
  775. q1 >>= scale;
  776. mul >>= scale;
  777. /* Get the remainder */
  778. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  779. /* Case 2 */
  780. q2 = DIV_ROUND_UP(fref2, rate);
  781. r2 = abs(fref2 / q2 - rate);
  782. /*
  783. * Choose the best between two: less remainder we have the better. We
  784. * can't go case 2 if q2 is greater than 256 since SCR register can
  785. * hold only values 0 .. 255.
  786. */
  787. if (r2 >= r1 || q2 > 256) {
  788. /* case 1 is better */
  789. r = r1;
  790. q = q1;
  791. } else {
  792. /* case 2 is better */
  793. r = r2;
  794. q = q2;
  795. mul = (1 << 24) * 2 / 5;
  796. }
  797. /* Check case 3 only if the divisor is big enough */
  798. if (fref / rate >= 80) {
  799. u64 fssp;
  800. u32 m;
  801. /* Calculate initial quot */
  802. q1 = DIV_ROUND_UP(fref, rate);
  803. m = (1 << 24) / q1;
  804. /* Get the remainder */
  805. fssp = (u64)fref * m;
  806. do_div(fssp, 1 << 24);
  807. r1 = abs(fssp - rate);
  808. /* Choose this one if it suits better */
  809. if (r1 < r) {
  810. /* case 3 is better */
  811. q = 1;
  812. mul = m;
  813. }
  814. }
  815. *dds = mul;
  816. return q - 1;
  817. }
  818. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  819. {
  820. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  821. const struct ssp_device *ssp = drv_data->ssp;
  822. rate = min_t(int, ssp_clk, rate);
  823. /*
  824. * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
  825. * that the SSP transmission rate can be greater than the device rate
  826. */
  827. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  828. return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
  829. else
  830. return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
  831. }
  832. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  833. int rate)
  834. {
  835. struct chip_data *chip =
  836. spi_get_ctldata(drv_data->master->cur_msg->spi);
  837. unsigned int clk_div;
  838. switch (drv_data->ssp_type) {
  839. case QUARK_X1000_SSP:
  840. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  841. break;
  842. default:
  843. clk_div = ssp_get_clk_div(drv_data, rate);
  844. break;
  845. }
  846. return clk_div << 8;
  847. }
  848. static bool pxa2xx_spi_can_dma(struct spi_master *master,
  849. struct spi_device *spi,
  850. struct spi_transfer *xfer)
  851. {
  852. struct chip_data *chip = spi_get_ctldata(spi);
  853. return chip->enable_dma &&
  854. xfer->len <= MAX_DMA_LEN &&
  855. xfer->len >= chip->dma_burst_size;
  856. }
  857. static void pump_transfers(unsigned long data)
  858. {
  859. struct driver_data *drv_data = (struct driver_data *)data;
  860. struct spi_master *master = drv_data->master;
  861. struct spi_message *message = master->cur_msg;
  862. struct chip_data *chip = spi_get_ctldata(message->spi);
  863. u32 dma_thresh = chip->dma_threshold;
  864. u32 dma_burst = chip->dma_burst_size;
  865. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  866. struct spi_transfer *transfer;
  867. struct spi_transfer *previous;
  868. u32 clk_div;
  869. u8 bits;
  870. u32 speed;
  871. u32 cr0;
  872. u32 cr1;
  873. int err;
  874. int dma_mapped;
  875. /* Get current state information */
  876. transfer = drv_data->cur_transfer;
  877. /* Handle for abort */
  878. if (message->state == ERROR_STATE) {
  879. message->status = -EIO;
  880. giveback(drv_data);
  881. return;
  882. }
  883. /* Handle end of message */
  884. if (message->state == DONE_STATE) {
  885. message->status = 0;
  886. giveback(drv_data);
  887. return;
  888. }
  889. /* Delay if requested at end of transfer before CS change */
  890. if (message->state == RUNNING_STATE) {
  891. previous = list_entry(transfer->transfer_list.prev,
  892. struct spi_transfer,
  893. transfer_list);
  894. if (previous->delay_usecs)
  895. udelay(previous->delay_usecs);
  896. /* Drop chip select only if cs_change is requested */
  897. if (previous->cs_change)
  898. cs_deassert(drv_data);
  899. }
  900. /* Check if we can DMA this transfer */
  901. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  902. /* reject already-mapped transfers; PIO won't always work */
  903. if (message->is_dma_mapped
  904. || transfer->rx_dma || transfer->tx_dma) {
  905. dev_err(&drv_data->pdev->dev,
  906. "pump_transfers: mapped transfer length of "
  907. "%u is greater than %d\n",
  908. transfer->len, MAX_DMA_LEN);
  909. message->status = -EINVAL;
  910. giveback(drv_data);
  911. return;
  912. }
  913. /* warn ... we force this to PIO mode */
  914. dev_warn_ratelimited(&message->spi->dev,
  915. "pump_transfers: DMA disabled for transfer length %ld "
  916. "greater than %d\n",
  917. (long)drv_data->len, MAX_DMA_LEN);
  918. }
  919. /* Setup the transfer state based on the type of transfer */
  920. if (pxa2xx_spi_flush(drv_data) == 0) {
  921. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  922. message->status = -EIO;
  923. giveback(drv_data);
  924. return;
  925. }
  926. drv_data->n_bytes = chip->n_bytes;
  927. drv_data->tx = (void *)transfer->tx_buf;
  928. drv_data->tx_end = drv_data->tx + transfer->len;
  929. drv_data->rx = transfer->rx_buf;
  930. drv_data->rx_end = drv_data->rx + transfer->len;
  931. drv_data->len = transfer->len;
  932. drv_data->write = drv_data->tx ? chip->write : null_writer;
  933. drv_data->read = drv_data->rx ? chip->read : null_reader;
  934. /* Change speed and bit per word on a per transfer */
  935. bits = transfer->bits_per_word;
  936. speed = transfer->speed_hz;
  937. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  938. if (bits <= 8) {
  939. drv_data->n_bytes = 1;
  940. drv_data->read = drv_data->read != null_reader ?
  941. u8_reader : null_reader;
  942. drv_data->write = drv_data->write != null_writer ?
  943. u8_writer : null_writer;
  944. } else if (bits <= 16) {
  945. drv_data->n_bytes = 2;
  946. drv_data->read = drv_data->read != null_reader ?
  947. u16_reader : null_reader;
  948. drv_data->write = drv_data->write != null_writer ?
  949. u16_writer : null_writer;
  950. } else if (bits <= 32) {
  951. drv_data->n_bytes = 4;
  952. drv_data->read = drv_data->read != null_reader ?
  953. u32_reader : null_reader;
  954. drv_data->write = drv_data->write != null_writer ?
  955. u32_writer : null_writer;
  956. }
  957. /*
  958. * if bits/word is changed in dma mode, then must check the
  959. * thresholds and burst also
  960. */
  961. if (chip->enable_dma) {
  962. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  963. message->spi,
  964. bits, &dma_burst,
  965. &dma_thresh))
  966. dev_warn_ratelimited(&message->spi->dev,
  967. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  968. }
  969. message->state = RUNNING_STATE;
  970. dma_mapped = master->can_dma &&
  971. master->can_dma(master, message->spi, transfer) &&
  972. master->cur_msg_mapped;
  973. if (dma_mapped) {
  974. /* Ensure we have the correct interrupt handler */
  975. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  976. err = pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  977. if (err) {
  978. message->status = err;
  979. giveback(drv_data);
  980. return;
  981. }
  982. /* Clear status and start DMA engine */
  983. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  984. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  985. pxa2xx_spi_dma_start(drv_data);
  986. } else {
  987. /* Ensure we have the correct interrupt handler */
  988. drv_data->transfer_handler = interrupt_transfer;
  989. /* Clear status */
  990. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  991. write_SSSR_CS(drv_data, drv_data->clear_sr);
  992. }
  993. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  994. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  995. if (!pxa25x_ssp_comp(drv_data))
  996. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  997. master->max_speed_hz
  998. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  999. dma_mapped ? "DMA" : "PIO");
  1000. else
  1001. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  1002. master->max_speed_hz / 2
  1003. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  1004. dma_mapped ? "DMA" : "PIO");
  1005. if (is_lpss_ssp(drv_data)) {
  1006. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  1007. != chip->lpss_rx_threshold)
  1008. pxa2xx_spi_write(drv_data, SSIRF,
  1009. chip->lpss_rx_threshold);
  1010. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  1011. != chip->lpss_tx_threshold)
  1012. pxa2xx_spi_write(drv_data, SSITF,
  1013. chip->lpss_tx_threshold);
  1014. }
  1015. if (is_quark_x1000_ssp(drv_data) &&
  1016. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  1017. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  1018. /* see if we need to reload the config registers */
  1019. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  1020. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  1021. != (cr1 & change_mask)) {
  1022. /* stop the SSP, and update the other bits */
  1023. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  1024. if (!pxa25x_ssp_comp(drv_data))
  1025. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  1026. /* first set CR1 without interrupt and service enables */
  1027. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  1028. /* restart the SSP */
  1029. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  1030. } else {
  1031. if (!pxa25x_ssp_comp(drv_data))
  1032. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  1033. }
  1034. cs_assert(drv_data);
  1035. /* after chip select, release the data by enabling service
  1036. * requests and interrupts, without changing any mode bits */
  1037. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  1038. }
  1039. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  1040. struct spi_message *msg)
  1041. {
  1042. struct driver_data *drv_data = spi_master_get_devdata(master);
  1043. /* Initial message state*/
  1044. msg->state = START_STATE;
  1045. drv_data->cur_transfer = list_entry(msg->transfers.next,
  1046. struct spi_transfer,
  1047. transfer_list);
  1048. /* Mark as busy and launch transfers */
  1049. tasklet_schedule(&drv_data->pump_transfers);
  1050. return 0;
  1051. }
  1052. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  1053. {
  1054. struct driver_data *drv_data = spi_master_get_devdata(master);
  1055. /* Disable the SSP now */
  1056. pxa2xx_spi_write(drv_data, SSCR0,
  1057. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  1058. return 0;
  1059. }
  1060. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  1061. struct pxa2xx_spi_chip *chip_info)
  1062. {
  1063. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1064. struct gpio_desc *gpiod;
  1065. int err = 0;
  1066. if (chip == NULL)
  1067. return 0;
  1068. if (drv_data->cs_gpiods) {
  1069. gpiod = drv_data->cs_gpiods[spi->chip_select];
  1070. if (gpiod) {
  1071. chip->gpiod_cs = gpiod;
  1072. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1073. gpiod_set_value(gpiod, chip->gpio_cs_inverted);
  1074. }
  1075. return 0;
  1076. }
  1077. if (chip_info == NULL)
  1078. return 0;
  1079. /* NOTE: setup() can be called multiple times, possibly with
  1080. * different chip_info, release previously requested GPIO
  1081. */
  1082. if (chip->gpiod_cs) {
  1083. gpio_free(desc_to_gpio(chip->gpiod_cs));
  1084. chip->gpiod_cs = NULL;
  1085. }
  1086. /* If (*cs_control) is provided, ignore GPIO chip select */
  1087. if (chip_info->cs_control) {
  1088. chip->cs_control = chip_info->cs_control;
  1089. return 0;
  1090. }
  1091. if (gpio_is_valid(chip_info->gpio_cs)) {
  1092. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1093. if (err) {
  1094. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1095. chip_info->gpio_cs);
  1096. return err;
  1097. }
  1098. gpiod = gpio_to_desc(chip_info->gpio_cs);
  1099. chip->gpiod_cs = gpiod;
  1100. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1101. err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
  1102. }
  1103. return err;
  1104. }
  1105. static int setup(struct spi_device *spi)
  1106. {
  1107. struct pxa2xx_spi_chip *chip_info;
  1108. struct chip_data *chip;
  1109. const struct lpss_config *config;
  1110. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1111. uint tx_thres, tx_hi_thres, rx_thres;
  1112. switch (drv_data->ssp_type) {
  1113. case QUARK_X1000_SSP:
  1114. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1115. tx_hi_thres = 0;
  1116. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1117. break;
  1118. case CE4100_SSP:
  1119. tx_thres = TX_THRESH_CE4100_DFLT;
  1120. tx_hi_thres = 0;
  1121. rx_thres = RX_THRESH_CE4100_DFLT;
  1122. break;
  1123. case LPSS_LPT_SSP:
  1124. case LPSS_BYT_SSP:
  1125. case LPSS_BSW_SSP:
  1126. case LPSS_SPT_SSP:
  1127. case LPSS_BXT_SSP:
  1128. case LPSS_CNL_SSP:
  1129. config = lpss_get_config(drv_data);
  1130. tx_thres = config->tx_threshold_lo;
  1131. tx_hi_thres = config->tx_threshold_hi;
  1132. rx_thres = config->rx_threshold;
  1133. break;
  1134. default:
  1135. tx_thres = TX_THRESH_DFLT;
  1136. tx_hi_thres = 0;
  1137. rx_thres = RX_THRESH_DFLT;
  1138. break;
  1139. }
  1140. /* Only alloc on first setup */
  1141. chip = spi_get_ctldata(spi);
  1142. if (!chip) {
  1143. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1144. if (!chip)
  1145. return -ENOMEM;
  1146. if (drv_data->ssp_type == CE4100_SSP) {
  1147. if (spi->chip_select > 4) {
  1148. dev_err(&spi->dev,
  1149. "failed setup: cs number must not be > 4.\n");
  1150. kfree(chip);
  1151. return -EINVAL;
  1152. }
  1153. chip->frm = spi->chip_select;
  1154. }
  1155. chip->enable_dma = drv_data->master_info->enable_dma;
  1156. chip->timeout = TIMOUT_DFLT;
  1157. }
  1158. /* protocol drivers may change the chip settings, so...
  1159. * if chip_info exists, use it */
  1160. chip_info = spi->controller_data;
  1161. /* chip_info isn't always needed */
  1162. chip->cr1 = 0;
  1163. if (chip_info) {
  1164. if (chip_info->timeout)
  1165. chip->timeout = chip_info->timeout;
  1166. if (chip_info->tx_threshold)
  1167. tx_thres = chip_info->tx_threshold;
  1168. if (chip_info->tx_hi_threshold)
  1169. tx_hi_thres = chip_info->tx_hi_threshold;
  1170. if (chip_info->rx_threshold)
  1171. rx_thres = chip_info->rx_threshold;
  1172. chip->dma_threshold = 0;
  1173. if (chip_info->enable_loopback)
  1174. chip->cr1 = SSCR1_LBM;
  1175. }
  1176. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1177. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1178. | SSITF_TxHiThresh(tx_hi_thres);
  1179. /* set dma burst and threshold outside of chip_info path so that if
  1180. * chip_info goes away after setting chip->enable_dma, the
  1181. * burst and threshold can still respond to changes in bits_per_word */
  1182. if (chip->enable_dma) {
  1183. /* set up legal burst and threshold for dma */
  1184. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1185. spi->bits_per_word,
  1186. &chip->dma_burst_size,
  1187. &chip->dma_threshold)) {
  1188. dev_warn(&spi->dev,
  1189. "in setup: DMA burst size reduced to match bits_per_word\n");
  1190. }
  1191. }
  1192. switch (drv_data->ssp_type) {
  1193. case QUARK_X1000_SSP:
  1194. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1195. & QUARK_X1000_SSCR1_RFT)
  1196. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1197. & QUARK_X1000_SSCR1_TFT);
  1198. break;
  1199. case CE4100_SSP:
  1200. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1201. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1202. break;
  1203. default:
  1204. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1205. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1206. break;
  1207. }
  1208. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1209. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1210. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1211. if (spi->mode & SPI_LOOP)
  1212. chip->cr1 |= SSCR1_LBM;
  1213. if (spi->bits_per_word <= 8) {
  1214. chip->n_bytes = 1;
  1215. chip->read = u8_reader;
  1216. chip->write = u8_writer;
  1217. } else if (spi->bits_per_word <= 16) {
  1218. chip->n_bytes = 2;
  1219. chip->read = u16_reader;
  1220. chip->write = u16_writer;
  1221. } else if (spi->bits_per_word <= 32) {
  1222. chip->n_bytes = 4;
  1223. chip->read = u32_reader;
  1224. chip->write = u32_writer;
  1225. }
  1226. spi_set_ctldata(spi, chip);
  1227. if (drv_data->ssp_type == CE4100_SSP)
  1228. return 0;
  1229. return setup_cs(spi, chip, chip_info);
  1230. }
  1231. static void cleanup(struct spi_device *spi)
  1232. {
  1233. struct chip_data *chip = spi_get_ctldata(spi);
  1234. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1235. if (!chip)
  1236. return;
  1237. if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
  1238. chip->gpiod_cs)
  1239. gpio_free(desc_to_gpio(chip->gpiod_cs));
  1240. kfree(chip);
  1241. }
  1242. #ifdef CONFIG_PCI
  1243. #ifdef CONFIG_ACPI
  1244. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1245. { "INT33C0", LPSS_LPT_SSP },
  1246. { "INT33C1", LPSS_LPT_SSP },
  1247. { "INT3430", LPSS_LPT_SSP },
  1248. { "INT3431", LPSS_LPT_SSP },
  1249. { "80860F0E", LPSS_BYT_SSP },
  1250. { "8086228E", LPSS_BSW_SSP },
  1251. { },
  1252. };
  1253. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1254. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1255. {
  1256. unsigned int devid;
  1257. int port_id = -1;
  1258. if (adev && adev->pnp.unique_id &&
  1259. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1260. port_id = devid;
  1261. return port_id;
  1262. }
  1263. #else /* !CONFIG_ACPI */
  1264. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1265. {
  1266. return -1;
  1267. }
  1268. #endif
  1269. /*
  1270. * PCI IDs of compound devices that integrate both host controller and private
  1271. * integrated DMA engine. Please note these are not used in module
  1272. * autoloading and probing in this module but matching the LPSS SSP type.
  1273. */
  1274. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1275. /* SPT-LP */
  1276. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1277. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1278. /* SPT-H */
  1279. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1280. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1281. /* KBL-H */
  1282. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1283. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1284. /* BXT A-Step */
  1285. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1286. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1287. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1288. /* BXT B-Step */
  1289. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1290. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1291. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1292. /* GLK */
  1293. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1294. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1295. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1296. /* ICL-LP */
  1297. { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
  1298. { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
  1299. { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
  1300. /* APL */
  1301. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1302. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1303. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1304. /* CNL-LP */
  1305. { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
  1306. { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
  1307. { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
  1308. /* CNL-H */
  1309. { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
  1310. { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
  1311. { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
  1312. { },
  1313. };
  1314. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1315. {
  1316. return param == chan->device->dev;
  1317. }
  1318. static struct pxa2xx_spi_master *
  1319. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1320. {
  1321. struct pxa2xx_spi_master *pdata;
  1322. struct acpi_device *adev;
  1323. struct ssp_device *ssp;
  1324. struct resource *res;
  1325. const struct acpi_device_id *adev_id = NULL;
  1326. const struct pci_device_id *pcidev_id = NULL;
  1327. int type;
  1328. adev = ACPI_COMPANION(&pdev->dev);
  1329. if (dev_is_pci(pdev->dev.parent))
  1330. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1331. to_pci_dev(pdev->dev.parent));
  1332. else if (adev)
  1333. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1334. &pdev->dev);
  1335. else
  1336. return NULL;
  1337. if (adev_id)
  1338. type = (int)adev_id->driver_data;
  1339. else if (pcidev_id)
  1340. type = (int)pcidev_id->driver_data;
  1341. else
  1342. return NULL;
  1343. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1344. if (!pdata)
  1345. return NULL;
  1346. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1347. if (!res)
  1348. return NULL;
  1349. ssp = &pdata->ssp;
  1350. ssp->phys_base = res->start;
  1351. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1352. if (IS_ERR(ssp->mmio_base))
  1353. return NULL;
  1354. if (pcidev_id) {
  1355. pdata->tx_param = pdev->dev.parent;
  1356. pdata->rx_param = pdev->dev.parent;
  1357. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1358. }
  1359. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1360. if (IS_ERR(ssp->clk))
  1361. return NULL;
  1362. ssp->irq = platform_get_irq(pdev, 0);
  1363. if (ssp->irq < 0)
  1364. return NULL;
  1365. ssp->type = type;
  1366. ssp->pdev = pdev;
  1367. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1368. pdata->num_chipselect = 1;
  1369. pdata->enable_dma = true;
  1370. return pdata;
  1371. }
  1372. #else /* !CONFIG_PCI */
  1373. static inline struct pxa2xx_spi_master *
  1374. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1375. {
  1376. return NULL;
  1377. }
  1378. #endif
  1379. static int pxa2xx_spi_fw_translate_cs(struct spi_master *master, unsigned cs)
  1380. {
  1381. struct driver_data *drv_data = spi_master_get_devdata(master);
  1382. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1383. switch (drv_data->ssp_type) {
  1384. /*
  1385. * For Atoms the ACPI DeviceSelection used by the Windows
  1386. * driver starts from 1 instead of 0 so translate it here
  1387. * to match what Linux expects.
  1388. */
  1389. case LPSS_BYT_SSP:
  1390. case LPSS_BSW_SSP:
  1391. return cs - 1;
  1392. default:
  1393. break;
  1394. }
  1395. }
  1396. return cs;
  1397. }
  1398. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1399. {
  1400. struct device *dev = &pdev->dev;
  1401. struct pxa2xx_spi_master *platform_info;
  1402. struct spi_master *master;
  1403. struct driver_data *drv_data;
  1404. struct ssp_device *ssp;
  1405. const struct lpss_config *config;
  1406. int status, count;
  1407. u32 tmp;
  1408. platform_info = dev_get_platdata(dev);
  1409. if (!platform_info) {
  1410. platform_info = pxa2xx_spi_init_pdata(pdev);
  1411. if (!platform_info) {
  1412. dev_err(&pdev->dev, "missing platform data\n");
  1413. return -ENODEV;
  1414. }
  1415. }
  1416. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1417. if (!ssp)
  1418. ssp = &platform_info->ssp;
  1419. if (!ssp->mmio_base) {
  1420. dev_err(&pdev->dev, "failed to get ssp\n");
  1421. return -ENODEV;
  1422. }
  1423. master = devm_spi_alloc_master(dev, sizeof(*drv_data));
  1424. if (!master) {
  1425. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1426. pxa_ssp_free(ssp);
  1427. return -ENOMEM;
  1428. }
  1429. drv_data = spi_master_get_devdata(master);
  1430. drv_data->master = master;
  1431. drv_data->master_info = platform_info;
  1432. drv_data->pdev = pdev;
  1433. drv_data->ssp = ssp;
  1434. master->dev.of_node = pdev->dev.of_node;
  1435. /* the spi->mode bits understood by this driver: */
  1436. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1437. master->bus_num = ssp->port_id;
  1438. master->dma_alignment = DMA_ALIGNMENT;
  1439. master->cleanup = cleanup;
  1440. master->setup = setup;
  1441. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1442. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1443. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1444. master->auto_runtime_pm = true;
  1445. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  1446. drv_data->ssp_type = ssp->type;
  1447. drv_data->ioaddr = ssp->mmio_base;
  1448. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1449. if (pxa25x_ssp_comp(drv_data)) {
  1450. switch (drv_data->ssp_type) {
  1451. case QUARK_X1000_SSP:
  1452. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1453. break;
  1454. default:
  1455. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1456. break;
  1457. }
  1458. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1459. drv_data->dma_cr1 = 0;
  1460. drv_data->clear_sr = SSSR_ROR;
  1461. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1462. } else {
  1463. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1464. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1465. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1466. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1467. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1468. }
  1469. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1470. drv_data);
  1471. if (status < 0) {
  1472. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1473. goto out_error_master_alloc;
  1474. }
  1475. /* Setup DMA if requested */
  1476. if (platform_info->enable_dma) {
  1477. status = pxa2xx_spi_dma_setup(drv_data);
  1478. if (status) {
  1479. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1480. platform_info->enable_dma = false;
  1481. } else {
  1482. master->can_dma = pxa2xx_spi_can_dma;
  1483. master->max_dma_len = MAX_DMA_LEN;
  1484. }
  1485. }
  1486. /* Enable SOC clock */
  1487. clk_prepare_enable(ssp->clk);
  1488. master->max_speed_hz = clk_get_rate(ssp->clk);
  1489. /* Load default SSP configuration */
  1490. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1491. switch (drv_data->ssp_type) {
  1492. case QUARK_X1000_SSP:
  1493. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1494. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1495. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1496. /* using the Motorola SPI protocol and use 8 bit frame */
  1497. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1498. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1499. break;
  1500. case CE4100_SSP:
  1501. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1502. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1503. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1504. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1505. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1506. break;
  1507. default:
  1508. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1509. SSCR1_TxTresh(TX_THRESH_DFLT);
  1510. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1511. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1512. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1513. break;
  1514. }
  1515. if (!pxa25x_ssp_comp(drv_data))
  1516. pxa2xx_spi_write(drv_data, SSTO, 0);
  1517. if (!is_quark_x1000_ssp(drv_data))
  1518. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1519. if (is_lpss_ssp(drv_data)) {
  1520. lpss_ssp_setup(drv_data);
  1521. config = lpss_get_config(drv_data);
  1522. if (config->reg_capabilities >= 0) {
  1523. tmp = __lpss_ssp_read_priv(drv_data,
  1524. config->reg_capabilities);
  1525. tmp &= LPSS_CAPS_CS_EN_MASK;
  1526. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1527. platform_info->num_chipselect = ffz(tmp);
  1528. } else if (config->cs_num) {
  1529. platform_info->num_chipselect = config->cs_num;
  1530. }
  1531. }
  1532. master->num_chipselect = platform_info->num_chipselect;
  1533. count = gpiod_count(&pdev->dev, "cs");
  1534. if (count > 0) {
  1535. int i;
  1536. master->num_chipselect = max_t(int, count,
  1537. master->num_chipselect);
  1538. drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
  1539. master->num_chipselect, sizeof(struct gpio_desc *),
  1540. GFP_KERNEL);
  1541. if (!drv_data->cs_gpiods) {
  1542. status = -ENOMEM;
  1543. goto out_error_clock_enabled;
  1544. }
  1545. for (i = 0; i < master->num_chipselect; i++) {
  1546. struct gpio_desc *gpiod;
  1547. gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
  1548. if (IS_ERR(gpiod)) {
  1549. /* Means use native chip select */
  1550. if (PTR_ERR(gpiod) == -ENOENT)
  1551. continue;
  1552. status = (int)PTR_ERR(gpiod);
  1553. goto out_error_clock_enabled;
  1554. } else {
  1555. drv_data->cs_gpiods[i] = gpiod;
  1556. }
  1557. }
  1558. }
  1559. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1560. (unsigned long)drv_data);
  1561. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1562. pm_runtime_use_autosuspend(&pdev->dev);
  1563. pm_runtime_set_active(&pdev->dev);
  1564. pm_runtime_enable(&pdev->dev);
  1565. /* Register with the SPI framework */
  1566. platform_set_drvdata(pdev, drv_data);
  1567. status = spi_register_master(master);
  1568. if (status != 0) {
  1569. dev_err(&pdev->dev, "problem registering spi master\n");
  1570. goto out_error_clock_enabled;
  1571. }
  1572. return status;
  1573. out_error_clock_enabled:
  1574. clk_disable_unprepare(ssp->clk);
  1575. pxa2xx_spi_dma_release(drv_data);
  1576. free_irq(ssp->irq, drv_data);
  1577. out_error_master_alloc:
  1578. pxa_ssp_free(ssp);
  1579. return status;
  1580. }
  1581. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1582. {
  1583. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1584. struct ssp_device *ssp;
  1585. if (!drv_data)
  1586. return 0;
  1587. ssp = drv_data->ssp;
  1588. pm_runtime_get_sync(&pdev->dev);
  1589. spi_unregister_master(drv_data->master);
  1590. /* Disable the SSP at the peripheral and SOC level */
  1591. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1592. clk_disable_unprepare(ssp->clk);
  1593. /* Release DMA */
  1594. if (drv_data->master_info->enable_dma)
  1595. pxa2xx_spi_dma_release(drv_data);
  1596. pm_runtime_put_noidle(&pdev->dev);
  1597. pm_runtime_disable(&pdev->dev);
  1598. /* Release IRQ */
  1599. free_irq(ssp->irq, drv_data);
  1600. /* Release SSP */
  1601. pxa_ssp_free(ssp);
  1602. return 0;
  1603. }
  1604. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1605. {
  1606. int status = 0;
  1607. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1608. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1609. }
  1610. #ifdef CONFIG_PM_SLEEP
  1611. static int pxa2xx_spi_suspend(struct device *dev)
  1612. {
  1613. struct driver_data *drv_data = dev_get_drvdata(dev);
  1614. struct ssp_device *ssp = drv_data->ssp;
  1615. int status;
  1616. status = spi_master_suspend(drv_data->master);
  1617. if (status != 0)
  1618. return status;
  1619. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1620. if (!pm_runtime_suspended(dev))
  1621. clk_disable_unprepare(ssp->clk);
  1622. return 0;
  1623. }
  1624. static int pxa2xx_spi_resume(struct device *dev)
  1625. {
  1626. struct driver_data *drv_data = dev_get_drvdata(dev);
  1627. struct ssp_device *ssp = drv_data->ssp;
  1628. int status;
  1629. /* Enable the SSP clock */
  1630. if (!pm_runtime_suspended(dev))
  1631. clk_prepare_enable(ssp->clk);
  1632. /* Restore LPSS private register bits */
  1633. if (is_lpss_ssp(drv_data))
  1634. lpss_ssp_setup(drv_data);
  1635. /* Start the queue running */
  1636. status = spi_master_resume(drv_data->master);
  1637. if (status != 0) {
  1638. dev_err(dev, "problem starting queue (%d)\n", status);
  1639. return status;
  1640. }
  1641. return 0;
  1642. }
  1643. #endif
  1644. #ifdef CONFIG_PM
  1645. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1646. {
  1647. struct driver_data *drv_data = dev_get_drvdata(dev);
  1648. clk_disable_unprepare(drv_data->ssp->clk);
  1649. return 0;
  1650. }
  1651. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1652. {
  1653. struct driver_data *drv_data = dev_get_drvdata(dev);
  1654. clk_prepare_enable(drv_data->ssp->clk);
  1655. return 0;
  1656. }
  1657. #endif
  1658. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1659. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1660. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1661. pxa2xx_spi_runtime_resume, NULL)
  1662. };
  1663. static struct platform_driver driver = {
  1664. .driver = {
  1665. .name = "pxa2xx-spi",
  1666. .pm = &pxa2xx_spi_pm_ops,
  1667. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1668. },
  1669. .probe = pxa2xx_spi_probe,
  1670. .remove = pxa2xx_spi_remove,
  1671. .shutdown = pxa2xx_spi_shutdown,
  1672. };
  1673. static int __init pxa2xx_spi_init(void)
  1674. {
  1675. return platform_driver_register(&driver);
  1676. }
  1677. subsys_initcall(pxa2xx_spi_init);
  1678. static void __exit pxa2xx_spi_exit(void)
  1679. {
  1680. platform_driver_unregister(&driver);
  1681. }
  1682. module_exit(pxa2xx_spi_exit);