cx18-firmware.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cx18 firmware functions
  4. *
  5. * Copyright (C) 2007 Hans Verkuil <[email protected]>
  6. * Copyright (C) 2008 Andy Walls <[email protected]>
  7. */
  8. #include "cx18-driver.h"
  9. #include "cx18-io.h"
  10. #include "cx18-scb.h"
  11. #include "cx18-irq.h"
  12. #include "cx18-firmware.h"
  13. #include "cx18-cards.h"
  14. #include <linux/firmware.h>
  15. #define CX18_PROC_SOFT_RESET 0xc70010
  16. #define CX18_DDR_SOFT_RESET 0xc70014
  17. #define CX18_CLOCK_SELECT1 0xc71000
  18. #define CX18_CLOCK_SELECT2 0xc71004
  19. #define CX18_HALF_CLOCK_SELECT1 0xc71008
  20. #define CX18_HALF_CLOCK_SELECT2 0xc7100C
  21. #define CX18_CLOCK_POLARITY1 0xc71010
  22. #define CX18_CLOCK_POLARITY2 0xc71014
  23. #define CX18_ADD_DELAY_ENABLE1 0xc71018
  24. #define CX18_ADD_DELAY_ENABLE2 0xc7101C
  25. #define CX18_CLOCK_ENABLE1 0xc71020
  26. #define CX18_CLOCK_ENABLE2 0xc71024
  27. #define CX18_REG_BUS_TIMEOUT_EN 0xc72024
  28. #define CX18_FAST_CLOCK_PLL_INT 0xc78000
  29. #define CX18_FAST_CLOCK_PLL_FRAC 0xc78004
  30. #define CX18_FAST_CLOCK_PLL_POST 0xc78008
  31. #define CX18_FAST_CLOCK_PLL_PRESCALE 0xc7800C
  32. #define CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH 0xc78010
  33. #define CX18_SLOW_CLOCK_PLL_INT 0xc78014
  34. #define CX18_SLOW_CLOCK_PLL_FRAC 0xc78018
  35. #define CX18_SLOW_CLOCK_PLL_POST 0xc7801C
  36. #define CX18_MPEG_CLOCK_PLL_INT 0xc78040
  37. #define CX18_MPEG_CLOCK_PLL_FRAC 0xc78044
  38. #define CX18_MPEG_CLOCK_PLL_POST 0xc78048
  39. #define CX18_PLL_POWER_DOWN 0xc78088
  40. #define CX18_SW1_INT_STATUS 0xc73104
  41. #define CX18_SW1_INT_ENABLE_PCI 0xc7311C
  42. #define CX18_SW2_INT_SET 0xc73140
  43. #define CX18_SW2_INT_STATUS 0xc73144
  44. #define CX18_ADEC_CONTROL 0xc78120
  45. #define CX18_DDR_REQUEST_ENABLE 0xc80000
  46. #define CX18_DDR_CHIP_CONFIG 0xc80004
  47. #define CX18_DDR_REFRESH 0xc80008
  48. #define CX18_DDR_TIMING1 0xc8000C
  49. #define CX18_DDR_TIMING2 0xc80010
  50. #define CX18_DDR_POWER_REG 0xc8001C
  51. #define CX18_DDR_TUNE_LANE 0xc80048
  52. #define CX18_DDR_INITIAL_EMRS 0xc80054
  53. #define CX18_DDR_MB_PER_ROW_7 0xc8009C
  54. #define CX18_DDR_BASE_63_ADDR 0xc804FC
  55. #define CX18_WMB_CLIENT02 0xc90108
  56. #define CX18_WMB_CLIENT05 0xc90114
  57. #define CX18_WMB_CLIENT06 0xc90118
  58. #define CX18_WMB_CLIENT07 0xc9011C
  59. #define CX18_WMB_CLIENT08 0xc90120
  60. #define CX18_WMB_CLIENT09 0xc90124
  61. #define CX18_WMB_CLIENT10 0xc90128
  62. #define CX18_WMB_CLIENT11 0xc9012C
  63. #define CX18_WMB_CLIENT12 0xc90130
  64. #define CX18_WMB_CLIENT13 0xc90134
  65. #define CX18_WMB_CLIENT14 0xc90138
  66. #define CX18_DSP0_INTERRUPT_MASK 0xd0004C
  67. #define APU_ROM_SYNC1 0x6D676553 /* "mgeS" */
  68. #define APU_ROM_SYNC2 0x72646548 /* "rdeH" */
  69. struct cx18_apu_rom_seghdr {
  70. u32 sync1;
  71. u32 sync2;
  72. u32 addr;
  73. u32 size;
  74. };
  75. static int load_cpu_fw_direct(const char *fn, u8 __iomem *mem, struct cx18 *cx)
  76. {
  77. const struct firmware *fw = NULL;
  78. int i, j;
  79. unsigned size;
  80. u32 __iomem *dst = (u32 __iomem *)mem;
  81. const u32 *src;
  82. if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {
  83. CX18_ERR("Unable to open firmware %s\n", fn);
  84. CX18_ERR("Did you put the firmware in the hotplug firmware directory?\n");
  85. return -ENOMEM;
  86. }
  87. src = (const u32 *)fw->data;
  88. for (i = 0; i < fw->size; i += 4096) {
  89. cx18_setup_page(cx, i);
  90. for (j = i; j < fw->size && j < i + 4096; j += 4) {
  91. /* no need for endianness conversion on the ppc */
  92. cx18_raw_writel(cx, *src, dst);
  93. if (cx18_raw_readl(cx, dst) != *src) {
  94. CX18_ERR("Mismatch at offset %x\n", i);
  95. release_firmware(fw);
  96. cx18_setup_page(cx, 0);
  97. return -EIO;
  98. }
  99. dst++;
  100. src++;
  101. }
  102. }
  103. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
  104. CX18_INFO("loaded %s firmware (%zu bytes)\n", fn, fw->size);
  105. size = fw->size;
  106. release_firmware(fw);
  107. cx18_setup_page(cx, SCB_OFFSET);
  108. return size;
  109. }
  110. static int load_apu_fw_direct(const char *fn, u8 __iomem *dst, struct cx18 *cx,
  111. u32 *entry_addr)
  112. {
  113. const struct firmware *fw = NULL;
  114. int i, j;
  115. unsigned size;
  116. const u32 *src;
  117. struct cx18_apu_rom_seghdr seghdr;
  118. const u8 *vers;
  119. u32 offset = 0;
  120. u32 apu_version = 0;
  121. int sz;
  122. if (request_firmware(&fw, fn, &cx->pci_dev->dev)) {
  123. CX18_ERR("unable to open firmware %s\n", fn);
  124. CX18_ERR("did you put the firmware in the hotplug firmware directory?\n");
  125. cx18_setup_page(cx, 0);
  126. return -ENOMEM;
  127. }
  128. *entry_addr = 0;
  129. src = (const u32 *)fw->data;
  130. vers = fw->data + sizeof(seghdr);
  131. sz = fw->size;
  132. apu_version = (vers[0] << 24) | (vers[4] << 16) | vers[32];
  133. while (offset + sizeof(seghdr) < fw->size) {
  134. const __le32 *shptr = (__force __le32 *)src + offset / 4;
  135. seghdr.sync1 = le32_to_cpu(shptr[0]);
  136. seghdr.sync2 = le32_to_cpu(shptr[1]);
  137. seghdr.addr = le32_to_cpu(shptr[2]);
  138. seghdr.size = le32_to_cpu(shptr[3]);
  139. offset += sizeof(seghdr);
  140. if (seghdr.sync1 != APU_ROM_SYNC1 ||
  141. seghdr.sync2 != APU_ROM_SYNC2) {
  142. offset += seghdr.size;
  143. continue;
  144. }
  145. CX18_DEBUG_INFO("load segment %x-%x\n", seghdr.addr,
  146. seghdr.addr + seghdr.size - 1);
  147. if (*entry_addr == 0)
  148. *entry_addr = seghdr.addr;
  149. if (offset + seghdr.size > sz)
  150. break;
  151. for (i = 0; i < seghdr.size; i += 4096) {
  152. cx18_setup_page(cx, seghdr.addr + i);
  153. for (j = i; j < seghdr.size && j < i + 4096; j += 4) {
  154. /* no need for endianness conversion on the ppc */
  155. cx18_raw_writel(cx, src[(offset + j) / 4],
  156. dst + seghdr.addr + j);
  157. if (cx18_raw_readl(cx, dst + seghdr.addr + j)
  158. != src[(offset + j) / 4]) {
  159. CX18_ERR("Mismatch at offset %x\n",
  160. offset + j);
  161. release_firmware(fw);
  162. cx18_setup_page(cx, 0);
  163. return -EIO;
  164. }
  165. }
  166. }
  167. offset += seghdr.size;
  168. }
  169. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags))
  170. CX18_INFO("loaded %s firmware V%08x (%zu bytes)\n",
  171. fn, apu_version, fw->size);
  172. size = fw->size;
  173. release_firmware(fw);
  174. cx18_setup_page(cx, 0);
  175. return size;
  176. }
  177. void cx18_halt_firmware(struct cx18 *cx)
  178. {
  179. CX18_DEBUG_INFO("Preparing for firmware halt.\n");
  180. cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
  181. 0x0000000F, 0x000F000F);
  182. cx18_write_reg_expect(cx, 0x00020002, CX18_ADEC_CONTROL,
  183. 0x00000002, 0x00020002);
  184. }
  185. void cx18_init_power(struct cx18 *cx, int lowpwr)
  186. {
  187. /* power-down Spare and AOM PLLs */
  188. /* power-up fast, slow and mpeg PLLs */
  189. cx18_write_reg(cx, 0x00000008, CX18_PLL_POWER_DOWN);
  190. /* ADEC out of sleep */
  191. cx18_write_reg_expect(cx, 0x00020000, CX18_ADEC_CONTROL,
  192. 0x00000000, 0x00020002);
  193. /*
  194. * The PLL parameters are based on the external crystal frequency that
  195. * would ideally be:
  196. *
  197. * NTSC Color subcarrier freq * 8 =
  198. * 4.5 MHz/286 * 455/2 * 8 = 28.63636363... MHz
  199. *
  200. * The accidents of history and rationale that explain from where this
  201. * combination of magic numbers originate can be found in:
  202. *
  203. * [1] Abrahams, I. C., "Choice of Chrominance Subcarrier Frequency in
  204. * the NTSC Standards", Proceedings of the I-R-E, January 1954, pp 79-80
  205. *
  206. * [2] Abrahams, I. C., "The 'Frequency Interleaving' Principle in the
  207. * NTSC Standards", Proceedings of the I-R-E, January 1954, pp 81-83
  208. *
  209. * As Mike Bradley has rightly pointed out, it's not the exact crystal
  210. * frequency that matters, only that all parts of the driver and
  211. * firmware are using the same value (close to the ideal value).
  212. *
  213. * Since I have a strong suspicion that, if the firmware ever assumes a
  214. * crystal value at all, it will assume 28.636360 MHz, the crystal
  215. * freq used in calculations in this driver will be:
  216. *
  217. * xtal_freq = 28.636360 MHz
  218. *
  219. * an error of less than 0.13 ppm which is way, way better than any off
  220. * the shelf crystal will have for accuracy anyway.
  221. *
  222. * Below I aim to run the PLLs' VCOs near 400 MHz to minimize errors.
  223. *
  224. * Many thanks to Jeff Campbell and Mike Bradley for their extensive
  225. * investigation, experimentation, testing, and suggested solutions of
  226. * audio/video sync problems with SVideo and CVBS captures.
  227. */
  228. /* the fast clock is at 200/245 MHz */
  229. /* 1 * xtal_freq * 0x0d.f7df9b8 / 2 = 200 MHz: 400 MHz pre post-divide*/
  230. /* 1 * xtal_freq * 0x11.1c71eb8 / 2 = 245 MHz: 490 MHz pre post-divide*/
  231. cx18_write_reg(cx, lowpwr ? 0xD : 0x11, CX18_FAST_CLOCK_PLL_INT);
  232. cx18_write_reg(cx, lowpwr ? 0x1EFBF37 : 0x038E3D7,
  233. CX18_FAST_CLOCK_PLL_FRAC);
  234. cx18_write_reg(cx, 2, CX18_FAST_CLOCK_PLL_POST);
  235. cx18_write_reg(cx, 1, CX18_FAST_CLOCK_PLL_PRESCALE);
  236. cx18_write_reg(cx, 4, CX18_FAST_CLOCK_PLL_ADJUST_BANDWIDTH);
  237. /* set slow clock to 125/120 MHz */
  238. /* xtal_freq * 0x0d.1861a20 / 3 = 125 MHz: 375 MHz before post-divide */
  239. /* xtal_freq * 0x0c.92493f8 / 3 = 120 MHz: 360 MHz before post-divide */
  240. cx18_write_reg(cx, lowpwr ? 0xD : 0xC, CX18_SLOW_CLOCK_PLL_INT);
  241. cx18_write_reg(cx, lowpwr ? 0x30C344 : 0x124927F,
  242. CX18_SLOW_CLOCK_PLL_FRAC);
  243. cx18_write_reg(cx, 3, CX18_SLOW_CLOCK_PLL_POST);
  244. /* mpeg clock pll 54MHz */
  245. /* xtal_freq * 0xf.15f17f0 / 8 = 54 MHz: 432 MHz before post-divide */
  246. cx18_write_reg(cx, 0xF, CX18_MPEG_CLOCK_PLL_INT);
  247. cx18_write_reg(cx, 0x2BE2FE, CX18_MPEG_CLOCK_PLL_FRAC);
  248. cx18_write_reg(cx, 8, CX18_MPEG_CLOCK_PLL_POST);
  249. /* Defaults */
  250. /* APU = SC or SC/2 = 125/62.5 */
  251. /* EPU = SC = 125 */
  252. /* DDR = FC = 180 */
  253. /* ENC = SC = 125 */
  254. /* AI1 = SC = 125 */
  255. /* VIM2 = disabled */
  256. /* PCI = FC/2 = 90 */
  257. /* AI2 = disabled */
  258. /* DEMUX = disabled */
  259. /* AO = SC/2 = 62.5 */
  260. /* SER = 54MHz */
  261. /* VFC = disabled */
  262. /* USB = disabled */
  263. if (lowpwr) {
  264. cx18_write_reg_expect(cx, 0xFFFF0020, CX18_CLOCK_SELECT1,
  265. 0x00000020, 0xFFFFFFFF);
  266. cx18_write_reg_expect(cx, 0xFFFF0004, CX18_CLOCK_SELECT2,
  267. 0x00000004, 0xFFFFFFFF);
  268. } else {
  269. /* This doesn't explicitly set every clock select */
  270. cx18_write_reg_expect(cx, 0x00060004, CX18_CLOCK_SELECT1,
  271. 0x00000004, 0x00060006);
  272. cx18_write_reg_expect(cx, 0x00060006, CX18_CLOCK_SELECT2,
  273. 0x00000006, 0x00060006);
  274. }
  275. cx18_write_reg_expect(cx, 0xFFFF0002, CX18_HALF_CLOCK_SELECT1,
  276. 0x00000002, 0xFFFFFFFF);
  277. cx18_write_reg_expect(cx, 0xFFFF0104, CX18_HALF_CLOCK_SELECT2,
  278. 0x00000104, 0xFFFFFFFF);
  279. cx18_write_reg_expect(cx, 0xFFFF9026, CX18_CLOCK_ENABLE1,
  280. 0x00009026, 0xFFFFFFFF);
  281. cx18_write_reg_expect(cx, 0xFFFF3105, CX18_CLOCK_ENABLE2,
  282. 0x00003105, 0xFFFFFFFF);
  283. }
  284. void cx18_init_memory(struct cx18 *cx)
  285. {
  286. cx18_msleep_timeout(10, 0);
  287. cx18_write_reg_expect(cx, 0x00010000, CX18_DDR_SOFT_RESET,
  288. 0x00000000, 0x00010001);
  289. cx18_msleep_timeout(10, 0);
  290. cx18_write_reg(cx, cx->card->ddr.chip_config, CX18_DDR_CHIP_CONFIG);
  291. cx18_msleep_timeout(10, 0);
  292. cx18_write_reg(cx, cx->card->ddr.refresh, CX18_DDR_REFRESH);
  293. cx18_write_reg(cx, cx->card->ddr.timing1, CX18_DDR_TIMING1);
  294. cx18_write_reg(cx, cx->card->ddr.timing2, CX18_DDR_TIMING2);
  295. cx18_msleep_timeout(10, 0);
  296. /* Initialize DQS pad time */
  297. cx18_write_reg(cx, cx->card->ddr.tune_lane, CX18_DDR_TUNE_LANE);
  298. cx18_write_reg(cx, cx->card->ddr.initial_emrs, CX18_DDR_INITIAL_EMRS);
  299. cx18_msleep_timeout(10, 0);
  300. cx18_write_reg_expect(cx, 0x00020000, CX18_DDR_SOFT_RESET,
  301. 0x00000000, 0x00020002);
  302. cx18_msleep_timeout(10, 0);
  303. /* use power-down mode when idle */
  304. cx18_write_reg(cx, 0x00000010, CX18_DDR_POWER_REG);
  305. cx18_write_reg_expect(cx, 0x00010001, CX18_REG_BUS_TIMEOUT_EN,
  306. 0x00000001, 0x00010001);
  307. cx18_write_reg(cx, 0x48, CX18_DDR_MB_PER_ROW_7);
  308. cx18_write_reg(cx, 0xE0000, CX18_DDR_BASE_63_ADDR);
  309. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT02); /* AO */
  310. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT09); /* AI2 */
  311. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT05); /* VIM1 */
  312. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT06); /* AI1 */
  313. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT07); /* 3D comb */
  314. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT10); /* ME */
  315. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT12); /* ENC */
  316. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT13); /* PK */
  317. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT11); /* RC */
  318. cx18_write_reg(cx, 0x00000101, CX18_WMB_CLIENT14); /* AVO */
  319. }
  320. #define CX18_CPU_FIRMWARE "v4l-cx23418-cpu.fw"
  321. #define CX18_APU_FIRMWARE "v4l-cx23418-apu.fw"
  322. int cx18_firmware_init(struct cx18 *cx)
  323. {
  324. u32 fw_entry_addr;
  325. int sz, retries;
  326. u32 api_args[MAX_MB_ARGUMENTS];
  327. /* Allow chip to control CLKRUN */
  328. cx18_write_reg(cx, 0x5, CX18_DSP0_INTERRUPT_MASK);
  329. /* Stop the firmware */
  330. cx18_write_reg_expect(cx, 0x000F000F, CX18_PROC_SOFT_RESET,
  331. 0x0000000F, 0x000F000F);
  332. cx18_msleep_timeout(1, 0);
  333. /* If the CPU is still running */
  334. if ((cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 8) == 0) {
  335. CX18_ERR("%s: couldn't stop CPU to load firmware\n", __func__);
  336. return -EIO;
  337. }
  338. cx18_sw1_irq_enable(cx, IRQ_CPU_TO_EPU | IRQ_APU_TO_EPU);
  339. cx18_sw2_irq_enable(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
  340. sz = load_cpu_fw_direct(CX18_CPU_FIRMWARE, cx->enc_mem, cx);
  341. if (sz <= 0)
  342. return sz;
  343. /* The SCB & IPC area *must* be correct before starting the firmwares */
  344. cx18_init_scb(cx);
  345. fw_entry_addr = 0;
  346. sz = load_apu_fw_direct(CX18_APU_FIRMWARE, cx->enc_mem, cx,
  347. &fw_entry_addr);
  348. if (sz <= 0)
  349. return sz;
  350. /* Start the CPU. The CPU will take care of the APU for us. */
  351. cx18_write_reg_expect(cx, 0x00080000, CX18_PROC_SOFT_RESET,
  352. 0x00000000, 0x00080008);
  353. /* Wait up to 500 ms for the APU to come out of reset */
  354. for (retries = 0;
  355. retries < 50 && (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1;
  356. retries++)
  357. cx18_msleep_timeout(10, 0);
  358. cx18_msleep_timeout(200, 0);
  359. if (retries == 50 &&
  360. (cx18_read_reg(cx, CX18_PROC_SOFT_RESET) & 1) == 1) {
  361. CX18_ERR("Could not start the CPU\n");
  362. return -EIO;
  363. }
  364. /*
  365. * The CPU had once before set up to receive an interrupt for it's
  366. * outgoing IRQ_CPU_TO_EPU_ACK to us. If it ever does this, we get an
  367. * interrupt when it sends us an ack, but by the time we process it,
  368. * that flag in the SW2 status register has been cleared by the CPU
  369. * firmware. We'll prevent that not so useful condition from happening
  370. * by clearing the CPU's interrupt enables for Ack IRQ's we want to
  371. * process.
  372. */
  373. cx18_sw2_irq_disable_cpu(cx, IRQ_CPU_TO_EPU_ACK | IRQ_APU_TO_EPU_ACK);
  374. /* Try a benign command to see if the CPU is alive and well */
  375. sz = cx18_vapi_result(cx, api_args, CX18_CPU_DEBUG_PEEK32, 1, 0);
  376. if (sz < 0)
  377. return sz;
  378. /* initialize GPIO */
  379. cx18_write_reg_expect(cx, 0x14001400, 0xc78110, 0x00001400, 0x14001400);
  380. return 0;
  381. }
  382. MODULE_FIRMWARE(CX18_CPU_FIRMWARE);
  383. MODULE_FIRMWARE(CX18_APU_FIRMWARE);