cs5536.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB 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 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/types.h>
  19. #include <grub/cs5536.h>
  20. #include <grub/pci.h>
  21. #include <grub/time.h>
  22. #include <grub/ata.h>
  23. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  24. #include <grub/machine/kernel.h>
  25. #endif
  26. #include <grub/dl.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. /* Context for grub_cs5536_find. */
  29. struct grub_cs5536_find_ctx
  30. {
  31. grub_pci_device_t *devp;
  32. int found;
  33. };
  34. /* Helper for grub_cs5536_find. */
  35. static int
  36. grub_cs5536_find_iter (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
  37. {
  38. struct grub_cs5536_find_ctx *ctx = data;
  39. if (pciid == GRUB_CS5536_PCIID)
  40. {
  41. *ctx->devp = dev;
  42. ctx->found = 1;
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. int
  48. grub_cs5536_find (grub_pci_device_t *devp)
  49. {
  50. struct grub_cs5536_find_ctx ctx = {
  51. .devp = devp,
  52. .found = 0
  53. };
  54. grub_pci_iterate (grub_cs5536_find_iter, &ctx);
  55. return ctx.found;
  56. }
  57. grub_uint64_t
  58. grub_cs5536_read_msr (grub_pci_device_t dev, grub_uint32_t addr)
  59. {
  60. grub_uint64_t ret = 0;
  61. grub_pci_write (grub_pci_make_address (dev, GRUB_CS5536_MSR_MAILBOX_ADDR),
  62. addr);
  63. ret = (grub_uint64_t)
  64. grub_pci_read (grub_pci_make_address (dev, GRUB_CS5536_MSR_MAILBOX_DATA0));
  65. ret |= (((grub_uint64_t)
  66. grub_pci_read (grub_pci_make_address (dev,
  67. GRUB_CS5536_MSR_MAILBOX_DATA1)))
  68. << 32);
  69. return ret;
  70. }
  71. void
  72. grub_cs5536_write_msr (grub_pci_device_t dev, grub_uint32_t addr,
  73. grub_uint64_t val)
  74. {
  75. grub_pci_write (grub_pci_make_address (dev, GRUB_CS5536_MSR_MAILBOX_ADDR),
  76. addr);
  77. grub_pci_write (grub_pci_make_address (dev, GRUB_CS5536_MSR_MAILBOX_DATA0),
  78. val & 0xffffffff);
  79. grub_pci_write (grub_pci_make_address (dev, GRUB_CS5536_MSR_MAILBOX_DATA1),
  80. val >> 32);
  81. }
  82. grub_err_t
  83. grub_cs5536_smbus_wait (grub_port_t smbbase)
  84. {
  85. grub_uint64_t start = grub_get_time_ms ();
  86. while (1)
  87. {
  88. grub_uint8_t status;
  89. status = grub_inb (smbbase + GRUB_CS5536_SMB_REG_STATUS);
  90. if (status & GRUB_CS5536_SMB_REG_STATUS_SDAST)
  91. return GRUB_ERR_NONE;
  92. if (status & GRUB_CS5536_SMB_REG_STATUS_BER)
  93. return grub_error (GRUB_ERR_IO, "SM bus error");
  94. if (status & GRUB_CS5536_SMB_REG_STATUS_NACK)
  95. return grub_error (GRUB_ERR_IO, "NACK received");
  96. if (grub_get_time_ms () > start + 40)
  97. return grub_error (GRUB_ERR_IO, "SM stalled");
  98. }
  99. }
  100. grub_err_t
  101. grub_cs5536_read_spd_byte (grub_port_t smbbase, grub_uint8_t dev,
  102. grub_uint8_t addr, grub_uint8_t *res)
  103. {
  104. grub_err_t err;
  105. /* Send START. */
  106. grub_outb (grub_inb (smbbase + GRUB_CS5536_SMB_REG_CTRL1)
  107. | GRUB_CS5536_SMB_REG_CTRL1_START,
  108. smbbase + GRUB_CS5536_SMB_REG_CTRL1);
  109. /* Send device address. */
  110. err = grub_cs5536_smbus_wait (smbbase);
  111. if (err)
  112. return err;
  113. grub_outb (dev << 1, smbbase + GRUB_CS5536_SMB_REG_DATA);
  114. /* Send ACK. */
  115. err = grub_cs5536_smbus_wait (smbbase);
  116. if (err)
  117. return err;
  118. grub_outb (grub_inb (smbbase + GRUB_CS5536_SMB_REG_CTRL1)
  119. | GRUB_CS5536_SMB_REG_CTRL1_ACK,
  120. smbbase + GRUB_CS5536_SMB_REG_CTRL1);
  121. /* Send byte address. */
  122. grub_outb (addr, smbbase + GRUB_CS5536_SMB_REG_DATA);
  123. /* Send START. */
  124. err = grub_cs5536_smbus_wait (smbbase);
  125. if (err)
  126. return err;
  127. grub_outb (grub_inb (smbbase + GRUB_CS5536_SMB_REG_CTRL1)
  128. | GRUB_CS5536_SMB_REG_CTRL1_START,
  129. smbbase + GRUB_CS5536_SMB_REG_CTRL1);
  130. /* Send device address. */
  131. err = grub_cs5536_smbus_wait (smbbase);
  132. if (err)
  133. return err;
  134. grub_outb ((dev << 1) | 1, smbbase + GRUB_CS5536_SMB_REG_DATA);
  135. /* Send STOP. */
  136. err = grub_cs5536_smbus_wait (smbbase);
  137. if (err)
  138. return err;
  139. grub_outb (grub_inb (smbbase + GRUB_CS5536_SMB_REG_CTRL1)
  140. | GRUB_CS5536_SMB_REG_CTRL1_STOP,
  141. smbbase + GRUB_CS5536_SMB_REG_CTRL1);
  142. err = grub_cs5536_smbus_wait (smbbase);
  143. if (err)
  144. return err;
  145. *res = grub_inb (smbbase + GRUB_CS5536_SMB_REG_DATA);
  146. return GRUB_ERR_NONE;
  147. }
  148. grub_err_t
  149. grub_cs5536_init_smbus (grub_pci_device_t dev, grub_uint16_t divisor,
  150. grub_port_t *smbbase)
  151. {
  152. grub_uint64_t smbbar;
  153. smbbar = grub_cs5536_read_msr (dev, GRUB_CS5536_MSR_SMB_BAR);
  154. /* FIXME */
  155. if (!(smbbar & GRUB_CS5536_LBAR_ENABLE))
  156. return grub_error(GRUB_ERR_IO, "SMB controller not enabled\n");
  157. *smbbase = (smbbar & GRUB_CS5536_LBAR_ADDR_MASK) + GRUB_MACHINE_PCI_IO_BASE;
  158. if (divisor < 8)
  159. return grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid divisor");
  160. /* Disable SMB. */
  161. grub_outb (0, *smbbase + GRUB_CS5536_SMB_REG_CTRL2);
  162. /* Disable interrupts. */
  163. grub_outb (0, *smbbase + GRUB_CS5536_SMB_REG_CTRL1);
  164. /* Set as master. */
  165. grub_outb (GRUB_CS5536_SMB_REG_ADDR_MASTER,
  166. *smbbase + GRUB_CS5536_SMB_REG_ADDR);
  167. /* Launch. */
  168. grub_outb (((divisor >> 7) & 0xff), *smbbase + GRUB_CS5536_SMB_REG_CTRL3);
  169. grub_outb (((divisor << 1) & 0xfe) | GRUB_CS5536_SMB_REG_CTRL2_ENABLE,
  170. *smbbase + GRUB_CS5536_SMB_REG_CTRL2);
  171. return GRUB_ERR_NONE;
  172. }
  173. grub_err_t
  174. grub_cs5536_read_spd (grub_port_t smbbase, grub_uint8_t dev,
  175. struct grub_smbus_spd *res)
  176. {
  177. grub_err_t err;
  178. grub_size_t size;
  179. grub_uint8_t b;
  180. grub_size_t ptr;
  181. err = grub_cs5536_read_spd_byte (smbbase, dev, 0, &b);
  182. if (err)
  183. return err;
  184. if (b == 0)
  185. return grub_error (GRUB_ERR_IO, "no SPD found");
  186. size = b;
  187. ((grub_uint8_t *) res)[0] = b;
  188. for (ptr = 1; ptr < size; ptr++)
  189. {
  190. err = grub_cs5536_read_spd_byte (smbbase, dev, ptr,
  191. &((grub_uint8_t *) res)[ptr]);
  192. if (err)
  193. return err;
  194. }
  195. return GRUB_ERR_NONE;
  196. }
  197. static inline void
  198. set_io_space (grub_pci_device_t dev, int num, grub_uint16_t start,
  199. grub_uint16_t len)
  200. {
  201. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_GL_REGIONS_START + num,
  202. ((((grub_uint64_t) start + len - 4)
  203. << GRUB_CS5536_MSR_GL_REGION_IO_TOP_SHIFT)
  204. & GRUB_CS5536_MSR_GL_REGION_TOP_MASK)
  205. | (((grub_uint64_t) start
  206. << GRUB_CS5536_MSR_GL_REGION_IO_BASE_SHIFT)
  207. & GRUB_CS5536_MSR_GL_REGION_BASE_MASK)
  208. | GRUB_CS5536_MSR_GL_REGION_IO
  209. | GRUB_CS5536_MSR_GL_REGION_ENABLE);
  210. }
  211. static inline void
  212. set_iod (grub_pci_device_t dev, int num, int dest, int start, int mask)
  213. {
  214. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_GL_IOD_START + num,
  215. ((grub_uint64_t) dest << GRUB_CS5536_IOD_DEST_SHIFT)
  216. | (((grub_uint64_t) start & GRUB_CS5536_IOD_ADDR_MASK)
  217. << GRUB_CS5536_IOD_BASE_SHIFT)
  218. | ((mask & GRUB_CS5536_IOD_ADDR_MASK)
  219. << GRUB_CS5536_IOD_MASK_SHIFT));
  220. }
  221. static inline void
  222. set_p2d (grub_pci_device_t dev, int num, int dest, grub_uint32_t start)
  223. {
  224. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_GL_P2D_START + num,
  225. (((grub_uint64_t) dest) << GRUB_CS5536_P2D_DEST_SHIFT)
  226. | ((grub_uint64_t) (start >> GRUB_CS5536_P2D_LOG_ALIGN)
  227. << GRUB_CS5536_P2D_BASE_SHIFT)
  228. | (((1 << (32 - GRUB_CS5536_P2D_LOG_ALIGN)) - 1)
  229. << GRUB_CS5536_P2D_MASK_SHIFT));
  230. }
  231. void
  232. grub_cs5536_init_geode (grub_pci_device_t dev)
  233. {
  234. /* Enable more BARs. */
  235. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_IRQ_MAP_BAR,
  236. GRUB_CS5536_LBAR_TURN_ON | GRUB_CS5536_LBAR_IRQ_MAP);
  237. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_MFGPT_BAR,
  238. GRUB_CS5536_LBAR_TURN_ON | GRUB_CS5536_LBAR_MFGPT);
  239. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_ACPI_BAR,
  240. GRUB_CS5536_LBAR_TURN_ON | GRUB_CS5536_LBAR_ACPI);
  241. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_PM_BAR,
  242. GRUB_CS5536_LBAR_TURN_ON | GRUB_CS5536_LBAR_PM);
  243. /* Setup DIVIL. */
  244. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  245. switch (grub_arch_machine)
  246. {
  247. case GRUB_ARCH_MACHINE_YEELOONG:
  248. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_DIVIL_LEG_IO,
  249. GRUB_CS5536_MSR_DIVIL_LEG_IO_MODE_X86
  250. | GRUB_CS5536_MSR_DIVIL_LEG_IO_F_REMAP
  251. | GRUB_CS5536_MSR_DIVIL_LEG_IO_RTC_ENABLE0
  252. | GRUB_CS5536_MSR_DIVIL_LEG_IO_RTC_ENABLE1);
  253. break;
  254. case GRUB_ARCH_MACHINE_FULOONG2F:
  255. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_DIVIL_LEG_IO,
  256. GRUB_CS5536_MSR_DIVIL_LEG_IO_UART2_COM3
  257. | GRUB_CS5536_MSR_DIVIL_LEG_IO_UART1_COM1
  258. | GRUB_CS5536_MSR_DIVIL_LEG_IO_MODE_X86
  259. | GRUB_CS5536_MSR_DIVIL_LEG_IO_F_REMAP
  260. | GRUB_CS5536_MSR_DIVIL_LEG_IO_RTC_ENABLE0
  261. | GRUB_CS5536_MSR_DIVIL_LEG_IO_RTC_ENABLE1);
  262. break;
  263. }
  264. #endif
  265. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_DIVIL_IRQ_MAPPER_PRIMARY_MASK,
  266. (~GRUB_CS5536_DIVIL_LPC_INTERRUPTS) & 0xffff);
  267. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_DIVIL_IRQ_MAPPER_LPC_MASK,
  268. GRUB_CS5536_DIVIL_LPC_INTERRUPTS);
  269. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_DIVIL_LPC_SERIAL_IRQ_CONTROL,
  270. GRUB_CS5536_MSR_DIVIL_LPC_SERIAL_IRQ_CONTROL_ENABLE);
  271. /* Initialise USB controller. */
  272. /* FIXME: assign adresses dynamically. */
  273. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_USB_OHCI_BASE,
  274. GRUB_CS5536_MSR_USB_BASE_BUS_MASTER
  275. | GRUB_CS5536_MSR_USB_BASE_MEMORY_ENABLE
  276. | 0x05024000);
  277. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_USB_EHCI_BASE,
  278. GRUB_CS5536_MSR_USB_BASE_BUS_MASTER
  279. | GRUB_CS5536_MSR_USB_BASE_MEMORY_ENABLE
  280. | (0x20ULL << GRUB_CS5536_MSR_USB_EHCI_BASE_FLDJ_SHIFT)
  281. | 0x05023000);
  282. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_USB_CONTROLLER_BASE,
  283. GRUB_CS5536_MSR_USB_BASE_BUS_MASTER
  284. | GRUB_CS5536_MSR_USB_BASE_MEMORY_ENABLE | 0x05020000);
  285. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_USB_OPTION_CONTROLLER_BASE,
  286. GRUB_CS5536_MSR_USB_BASE_MEMORY_ENABLE | 0x05022000);
  287. set_p2d (dev, 0, GRUB_CS5536_DESTINATION_USB, 0x05020000);
  288. set_p2d (dev, 1, GRUB_CS5536_DESTINATION_USB, 0x05022000);
  289. set_p2d (dev, 5, GRUB_CS5536_DESTINATION_USB, 0x05024000);
  290. set_p2d (dev, 6, GRUB_CS5536_DESTINATION_USB, 0x05023000);
  291. {
  292. volatile grub_uint32_t *oc;
  293. oc = grub_absolute_pointer (grub_pci_device_map_range (dev, 0x05022000,
  294. GRUB_CS5536_USB_OPTION_REGS_SIZE));
  295. oc[GRUB_CS5536_USB_OPTION_REG_UOCMUX] =
  296. (oc[GRUB_CS5536_USB_OPTION_REG_UOCMUX]
  297. & ~GRUB_CS5536_USB_OPTION_REG_UOCMUX_PMUX_MASK)
  298. | GRUB_CS5536_USB_OPTION_REG_UOCMUX_PMUX_HC;
  299. grub_pci_device_unmap_range (dev, oc, GRUB_CS5536_USB_OPTION_REGS_SIZE);
  300. }
  301. /* Setup IDE controller. */
  302. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_IDE_IO_BAR,
  303. GRUB_CS5536_LBAR_IDE
  304. | GRUB_CS5536_MSR_IDE_IO_BAR_UNITS);
  305. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_IDE_CFG,
  306. GRUB_CS5536_MSR_IDE_CFG_CHANNEL_ENABLE);
  307. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_IDE_TIMING,
  308. (GRUB_CS5536_MSR_IDE_TIMING_PIO0
  309. << GRUB_CS5536_MSR_IDE_TIMING_DRIVE0_SHIFT)
  310. | (GRUB_CS5536_MSR_IDE_TIMING_PIO0
  311. << GRUB_CS5536_MSR_IDE_TIMING_DRIVE1_SHIFT));
  312. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_IDE_CAS_TIMING,
  313. (GRUB_CS5536_MSR_IDE_CAS_TIMING_CMD_PIO0
  314. << GRUB_CS5536_MSR_IDE_CAS_TIMING_CMD_SHIFT)
  315. | (GRUB_CS5536_MSR_IDE_CAS_TIMING_PIO0
  316. << GRUB_CS5536_MSR_IDE_CAS_TIMING_DRIVE0_SHIFT)
  317. | (GRUB_CS5536_MSR_IDE_CAS_TIMING_PIO0
  318. << GRUB_CS5536_MSR_IDE_CAS_TIMING_DRIVE1_SHIFT));
  319. /* Setup Geodelink PCI. */
  320. grub_cs5536_write_msr (dev, GRUB_CS5536_MSR_GL_PCI_CTRL,
  321. (4ULL << GRUB_CS5536_MSR_GL_PCI_CTRL_OUT_THR_SHIFT)
  322. | (4ULL << GRUB_CS5536_MSR_GL_PCI_CTRL_IN_THR_SHIFT)
  323. | (8ULL << GRUB_CS5536_MSR_GL_PCI_CTRL_LATENCY_SHIFT)
  324. | GRUB_CS5536_MSR_GL_PCI_CTRL_IO_ENABLE
  325. | GRUB_CS5536_MSR_GL_PCI_CTRL_MEMORY_ENABLE);
  326. /* Setup windows. */
  327. set_io_space (dev, 0, GRUB_CS5536_LBAR_SMBUS, GRUB_CS5536_SMBUS_REGS_SIZE);
  328. set_io_space (dev, 1, GRUB_CS5536_LBAR_GPIO, GRUB_CS5536_GPIO_REGS_SIZE);
  329. set_io_space (dev, 2, GRUB_CS5536_LBAR_MFGPT, GRUB_CS5536_MFGPT_REGS_SIZE);
  330. set_io_space (dev, 3, GRUB_CS5536_LBAR_IRQ_MAP, GRUB_CS5536_IRQ_MAP_REGS_SIZE);
  331. set_io_space (dev, 4, GRUB_CS5536_LBAR_PM, GRUB_CS5536_PM_REGS_SIZE);
  332. set_io_space (dev, 5, GRUB_CS5536_LBAR_ACPI, GRUB_CS5536_ACPI_REGS_SIZE);
  333. set_iod (dev, 0, GRUB_CS5536_DESTINATION_IDE, GRUB_ATA_CH0_PORT1, 0xffff8);
  334. set_iod (dev, 1, GRUB_CS5536_DESTINATION_ACC, GRUB_CS5536_LBAR_ACC, 0xfff80);
  335. set_iod (dev, 2, GRUB_CS5536_DESTINATION_IDE, GRUB_CS5536_LBAR_IDE, 0xffff0);
  336. }