core_marvel.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*
  2. * linux/arch/alpha/kernel/core_marvel.c
  3. *
  4. * Code common to all Marvel based systems.
  5. */
  6. #define __EXTERN_INLINE inline
  7. #include <asm/io.h>
  8. #include <asm/core_marvel.h>
  9. #undef __EXTERN_INLINE
  10. #include <linux/types.h>
  11. #include <linux/pci.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mc146818rtc.h>
  16. #include <linux/rtc.h>
  17. #include <linux/module.h>
  18. #include <linux/bootmem.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/smp.h>
  21. #include <asm/gct.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/rtc.h>
  25. #include <asm/vga.h>
  26. #include "proto.h"
  27. #include "pci_impl.h"
  28. /*
  29. * Debug helpers
  30. */
  31. #define DEBUG_CONFIG 0
  32. #if DEBUG_CONFIG
  33. # define DBG_CFG(args) printk args
  34. #else
  35. # define DBG_CFG(args)
  36. #endif
  37. /*
  38. * Private data
  39. */
  40. static struct io7 *io7_head = NULL;
  41. /*
  42. * Helper functions
  43. */
  44. static unsigned long __attribute__ ((unused))
  45. read_ev7_csr(int pe, unsigned long offset)
  46. {
  47. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  48. unsigned long q;
  49. mb();
  50. q = ev7csr->csr;
  51. mb();
  52. return q;
  53. }
  54. static void __attribute__ ((unused))
  55. write_ev7_csr(int pe, unsigned long offset, unsigned long q)
  56. {
  57. ev7_csr *ev7csr = EV7_CSR_KERN(pe, offset);
  58. mb();
  59. ev7csr->csr = q;
  60. mb();
  61. }
  62. static char * __init
  63. mk_resource_name(int pe, int port, char *str)
  64. {
  65. char tmp[80];
  66. char *name;
  67. sprintf(tmp, "PCI %s PE %d PORT %d", str, pe, port);
  68. name = alloc_bootmem(strlen(tmp) + 1);
  69. strcpy(name, tmp);
  70. return name;
  71. }
  72. inline struct io7 *
  73. marvel_next_io7(struct io7 *prev)
  74. {
  75. return (prev ? prev->next : io7_head);
  76. }
  77. struct io7 *
  78. marvel_find_io7(int pe)
  79. {
  80. struct io7 *io7;
  81. for (io7 = io7_head; io7 && io7->pe != pe; io7 = io7->next)
  82. continue;
  83. return io7;
  84. }
  85. static struct io7 * __init
  86. alloc_io7(unsigned int pe)
  87. {
  88. struct io7 *io7;
  89. struct io7 *insp;
  90. int h;
  91. if (marvel_find_io7(pe)) {
  92. printk(KERN_WARNING "IO7 at PE %d already allocated!\n", pe);
  93. return NULL;
  94. }
  95. io7 = alloc_bootmem(sizeof(*io7));
  96. io7->pe = pe;
  97. spin_lock_init(&io7->irq_lock);
  98. for (h = 0; h < 4; h++) {
  99. io7->ports[h].io7 = io7;
  100. io7->ports[h].port = h;
  101. io7->ports[h].enabled = 0; /* default to disabled */
  102. }
  103. /*
  104. * Insert in pe sorted order.
  105. */
  106. if (NULL == io7_head) /* empty list */
  107. io7_head = io7;
  108. else if (io7_head->pe > io7->pe) { /* insert at head */
  109. io7->next = io7_head;
  110. io7_head = io7;
  111. } else { /* insert at position */
  112. for (insp = io7_head; insp; insp = insp->next) {
  113. if (insp->pe == io7->pe) {
  114. printk(KERN_ERR "Too many IO7s at PE %d\n",
  115. io7->pe);
  116. return NULL;
  117. }
  118. if (NULL == insp->next ||
  119. insp->next->pe > io7->pe) { /* insert here */
  120. io7->next = insp->next;
  121. insp->next = io7;
  122. break;
  123. }
  124. }
  125. if (NULL == insp) { /* couldn't insert ?!? */
  126. printk(KERN_WARNING "Failed to insert IO7 at PE %d "
  127. " - adding at head of list\n", io7->pe);
  128. io7->next = io7_head;
  129. io7_head = io7;
  130. }
  131. }
  132. return io7;
  133. }
  134. void
  135. io7_clear_errors(struct io7 *io7)
  136. {
  137. io7_port7_csrs *p7csrs;
  138. io7_ioport_csrs *csrs;
  139. int port;
  140. /*
  141. * First the IO ports.
  142. */
  143. for (port = 0; port < 4; port++) {
  144. csrs = IO7_CSRS_KERN(io7->pe, port);
  145. csrs->POx_ERR_SUM.csr = -1UL;
  146. csrs->POx_TLB_ERR.csr = -1UL;
  147. csrs->POx_SPL_COMPLT.csr = -1UL;
  148. csrs->POx_TRANS_SUM.csr = -1UL;
  149. }
  150. /*
  151. * Then the common ones.
  152. */
  153. p7csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  154. p7csrs->PO7_ERROR_SUM.csr = -1UL;
  155. p7csrs->PO7_UNCRR_SYM.csr = -1UL;
  156. p7csrs->PO7_CRRCT_SYM.csr = -1UL;
  157. }
  158. /*
  159. * IO7 PCI, PCI/X, AGP configuration.
  160. */
  161. static void __init
  162. io7_init_hose(struct io7 *io7, int port)
  163. {
  164. static int hose_index = 0;
  165. struct pci_controller *hose = alloc_pci_controller();
  166. struct io7_port *io7_port = &io7->ports[port];
  167. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, port);
  168. int i;
  169. hose->index = hose_index++; /* arbitrary */
  170. /*
  171. * We don't have an isa or legacy hose, but glibc expects to be
  172. * able to use the bus == 0 / dev == 0 form of the iobase syscall
  173. * to determine information about the i/o system. Since XFree86
  174. * relies on glibc's determination to tell whether or not to use
  175. * sparse access, we need to point the pci_isa_hose at a real hose
  176. * so at least that determination is correct.
  177. */
  178. if (hose->index == 0)
  179. pci_isa_hose = hose;
  180. io7_port->csrs = csrs;
  181. io7_port->hose = hose;
  182. hose->sysdata = io7_port;
  183. hose->io_space = alloc_resource();
  184. hose->mem_space = alloc_resource();
  185. /*
  186. * Base addresses for userland consumption. Since these are going
  187. * to be mapped, they are pure physical addresses.
  188. */
  189. hose->sparse_mem_base = hose->sparse_io_base = 0;
  190. hose->dense_mem_base = IO7_MEM_PHYS(io7->pe, port);
  191. hose->dense_io_base = IO7_IO_PHYS(io7->pe, port);
  192. /*
  193. * Base addresses and resource ranges for kernel consumption.
  194. */
  195. hose->config_space_base = (unsigned long)IO7_CONF_KERN(io7->pe, port);
  196. hose->io_space->start = (unsigned long)IO7_IO_KERN(io7->pe, port);
  197. hose->io_space->end = hose->io_space->start + IO7_IO_SPACE - 1;
  198. hose->io_space->name = mk_resource_name(io7->pe, port, "IO");
  199. hose->io_space->flags = IORESOURCE_IO;
  200. hose->mem_space->start = (unsigned long)IO7_MEM_KERN(io7->pe, port);
  201. hose->mem_space->end = hose->mem_space->start + IO7_MEM_SPACE - 1;
  202. hose->mem_space->name = mk_resource_name(io7->pe, port, "MEM");
  203. hose->mem_space->flags = IORESOURCE_MEM;
  204. if (request_resource(&ioport_resource, hose->io_space) < 0)
  205. printk(KERN_ERR "Failed to request IO on hose %d\n",
  206. hose->index);
  207. if (request_resource(&iomem_resource, hose->mem_space) < 0)
  208. printk(KERN_ERR "Failed to request MEM on hose %d\n",
  209. hose->index);
  210. /*
  211. * Save the existing DMA window settings for later restoration.
  212. */
  213. for (i = 0; i < 4; i++) {
  214. io7_port->saved_wbase[i] = csrs->POx_WBASE[i].csr;
  215. io7_port->saved_wmask[i] = csrs->POx_WMASK[i].csr;
  216. io7_port->saved_tbase[i] = csrs->POx_TBASE[i].csr;
  217. }
  218. /*
  219. * Set up the PCI to main memory translation windows.
  220. *
  221. * Window 0 is scatter-gather 8MB at 8MB
  222. * Window 1 is direct access 1GB at 2GB
  223. * Window 2 is scatter-gather (up-to) 1GB at 3GB
  224. * Window 3 is disabled
  225. */
  226. /*
  227. * TBIA before modifying windows.
  228. */
  229. marvel_pci_tbi(hose, 0, -1);
  230. /*
  231. * Set up window 0 for scatter-gather 8MB at 8MB.
  232. */
  233. hose->sg_isa = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  234. hose, 0x00800000, 0x00800000, 0);
  235. hose->sg_isa->align_entry = 8; /* cache line boundary */
  236. csrs->POx_WBASE[0].csr =
  237. hose->sg_isa->dma_base | wbase_m_ena | wbase_m_sg;
  238. csrs->POx_WMASK[0].csr = (hose->sg_isa->size - 1) & wbase_m_addr;
  239. csrs->POx_TBASE[0].csr = virt_to_phys(hose->sg_isa->ptes);
  240. /*
  241. * Set up window 1 for direct-mapped 1GB at 2GB.
  242. */
  243. csrs->POx_WBASE[1].csr = __direct_map_base | wbase_m_ena;
  244. csrs->POx_WMASK[1].csr = (__direct_map_size - 1) & wbase_m_addr;
  245. csrs->POx_TBASE[1].csr = 0;
  246. /*
  247. * Set up window 2 for scatter-gather (up-to) 1GB at 3GB.
  248. */
  249. hose->sg_pci = iommu_arena_new_node(marvel_cpuid_to_nid(io7->pe),
  250. hose, 0xc0000000, 0x40000000, 0);
  251. hose->sg_pci->align_entry = 8; /* cache line boundary */
  252. csrs->POx_WBASE[2].csr =
  253. hose->sg_pci->dma_base | wbase_m_ena | wbase_m_sg;
  254. csrs->POx_WMASK[2].csr = (hose->sg_pci->size - 1) & wbase_m_addr;
  255. csrs->POx_TBASE[2].csr = virt_to_phys(hose->sg_pci->ptes);
  256. /*
  257. * Disable window 3.
  258. */
  259. csrs->POx_WBASE[3].csr = 0;
  260. /*
  261. * Make sure that the AGP Monster Window is disabled.
  262. */
  263. csrs->POx_CTRL.csr &= ~(1UL << 61);
  264. #if 1
  265. printk("FIXME: disabling master aborts\n");
  266. csrs->POx_MSK_HEI.csr &= ~(3UL << 14);
  267. #endif
  268. /*
  269. * TBIA after modifying windows.
  270. */
  271. marvel_pci_tbi(hose, 0, -1);
  272. }
  273. static void __init
  274. marvel_init_io7(struct io7 *io7)
  275. {
  276. int i;
  277. printk("Initializing IO7 at PID %d\n", io7->pe);
  278. /*
  279. * Get the Port 7 CSR pointer.
  280. */
  281. io7->csrs = IO7_PORT7_CSRS_KERN(io7->pe);
  282. /*
  283. * Init this IO7's hoses.
  284. */
  285. for (i = 0; i < IO7_NUM_PORTS; i++) {
  286. io7_ioport_csrs *csrs = IO7_CSRS_KERN(io7->pe, i);
  287. if (csrs->POx_CACHE_CTL.csr == 8) {
  288. io7->ports[i].enabled = 1;
  289. io7_init_hose(io7, i);
  290. }
  291. }
  292. }
  293. void
  294. marvel_io7_present(gct6_node *node)
  295. {
  296. int pe;
  297. if (node->type != GCT_TYPE_HOSE ||
  298. node->subtype != GCT_SUBTYPE_IO_PORT_MODULE)
  299. return;
  300. pe = (node->id >> 8) & 0xff;
  301. printk("Found an IO7 at PID %d\n", pe);
  302. alloc_io7(pe);
  303. }
  304. static void __init
  305. marvel_find_console_vga_hose(void)
  306. {
  307. u64 *pu64 = (u64 *)((u64)hwrpb + hwrpb->ctbt_offset);
  308. if (pu64[7] == 3) { /* TERM_TYPE == graphics */
  309. struct pci_controller *hose = NULL;
  310. int h = (pu64[30] >> 24) & 0xff; /* TERM_OUT_LOC, hose # */
  311. struct io7 *io7;
  312. int pid, port;
  313. /* FIXME - encoding is going to have to change for Marvel
  314. * since hose will be able to overflow a byte...
  315. * need to fix this decode when the console
  316. * changes its encoding
  317. */
  318. printk("console graphics is on hose %d (console)\n", h);
  319. /*
  320. * The console's hose numbering is:
  321. *
  322. * hose<n:2>: PID
  323. * hose<1:0>: PORT
  324. *
  325. * We need to find the hose at that pid and port
  326. */
  327. pid = h >> 2;
  328. port = h & 3;
  329. if ((io7 = marvel_find_io7(pid)))
  330. hose = io7->ports[port].hose;
  331. if (hose) {
  332. printk("Console graphics on hose %d\n", hose->index);
  333. pci_vga_hose = hose;
  334. }
  335. }
  336. }
  337. gct6_search_struct gct_wanted_node_list[] = {
  338. { GCT_TYPE_HOSE, GCT_SUBTYPE_IO_PORT_MODULE, marvel_io7_present },
  339. { 0, 0, NULL }
  340. };
  341. /*
  342. * In case the GCT is not complete, let the user specify PIDs with IO7s
  343. * at boot time. Syntax is 'io7=a,b,c,...,n' where a-n are the PIDs (decimal)
  344. * where IO7s are connected
  345. */
  346. static int __init
  347. marvel_specify_io7(char *str)
  348. {
  349. unsigned long pid;
  350. struct io7 *io7;
  351. char *pchar;
  352. do {
  353. pid = simple_strtoul(str, &pchar, 0);
  354. if (pchar != str) {
  355. printk("User-specified IO7 at PID %lu\n", pid);
  356. io7 = alloc_io7(pid);
  357. if (io7) marvel_init_io7(io7);
  358. }
  359. if (pchar == str) pchar++;
  360. str = pchar;
  361. } while(*str);
  362. return 1;
  363. }
  364. __setup("io7=", marvel_specify_io7);
  365. void __init
  366. marvel_init_arch(void)
  367. {
  368. struct io7 *io7;
  369. /* With multiple PCI busses, we play with I/O as physical addrs. */
  370. ioport_resource.end = ~0UL;
  371. /* PCI DMA Direct Mapping is 1GB at 2GB. */
  372. __direct_map_base = 0x80000000;
  373. __direct_map_size = 0x40000000;
  374. /* Parse the config tree. */
  375. gct6_find_nodes(GCT_NODE_PTR(0), gct_wanted_node_list);
  376. /* Init the io7s. */
  377. for (io7 = NULL; NULL != (io7 = marvel_next_io7(io7)); )
  378. marvel_init_io7(io7);
  379. /* Check for graphic console location (if any). */
  380. marvel_find_console_vga_hose();
  381. }
  382. void
  383. marvel_kill_arch(int mode)
  384. {
  385. }
  386. /*
  387. * PCI Configuration Space access functions
  388. *
  389. * Configuration space addresses have the following format:
  390. *
  391. * |2 2 2 2|1 1 1 1|1 1 1 1|1 1
  392. * |3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  393. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  394. * |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|R|R|
  395. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  396. *
  397. * n:24 reserved for hose base
  398. * 23:16 bus number (8 bits = 128 possible buses)
  399. * 15:11 Device number (5 bits)
  400. * 10:8 function number
  401. * 7:2 register number
  402. *
  403. * Notes:
  404. * IO7 determines whether to use a type 0 or type 1 config cycle
  405. * based on the bus number. Therefore the bus number must be set
  406. * to 0 for the root bus on any hose.
  407. *
  408. * The function number selects which function of a multi-function device
  409. * (e.g., SCSI and Ethernet).
  410. *
  411. */
  412. static inline unsigned long
  413. build_conf_addr(struct pci_controller *hose, u8 bus,
  414. unsigned int devfn, int where)
  415. {
  416. return (hose->config_space_base | (bus << 16) | (devfn << 8) | where);
  417. }
  418. static unsigned long
  419. mk_conf_addr(struct pci_bus *pbus, unsigned int devfn, int where)
  420. {
  421. struct pci_controller *hose = pbus->sysdata;
  422. struct io7_port *io7_port;
  423. unsigned long addr = 0;
  424. u8 bus = pbus->number;
  425. if (!hose)
  426. return addr;
  427. /* Check for enabled. */
  428. io7_port = hose->sysdata;
  429. if (!io7_port->enabled)
  430. return addr;
  431. if (!pbus->parent) { /* No parent means peer PCI bus. */
  432. /* Don't support idsel > 20 on primary bus. */
  433. if (devfn >= PCI_DEVFN(21, 0))
  434. return addr;
  435. bus = 0;
  436. }
  437. addr = build_conf_addr(hose, bus, devfn, where);
  438. DBG_CFG(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
  439. return addr;
  440. }
  441. static int
  442. marvel_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  443. int size, u32 *value)
  444. {
  445. unsigned long addr;
  446. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  447. return PCIBIOS_DEVICE_NOT_FOUND;
  448. switch(size) {
  449. case 1:
  450. *value = __kernel_ldbu(*(vucp)addr);
  451. break;
  452. case 2:
  453. *value = __kernel_ldwu(*(vusp)addr);
  454. break;
  455. case 4:
  456. *value = *(vuip)addr;
  457. break;
  458. default:
  459. return PCIBIOS_FUNC_NOT_SUPPORTED;
  460. }
  461. return PCIBIOS_SUCCESSFUL;
  462. }
  463. static int
  464. marvel_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  465. int size, u32 value)
  466. {
  467. unsigned long addr;
  468. if (0 == (addr = mk_conf_addr(bus, devfn, where)))
  469. return PCIBIOS_DEVICE_NOT_FOUND;
  470. switch (size) {
  471. case 1:
  472. __kernel_stb(value, *(vucp)addr);
  473. mb();
  474. __kernel_ldbu(*(vucp)addr);
  475. break;
  476. case 2:
  477. __kernel_stw(value, *(vusp)addr);
  478. mb();
  479. __kernel_ldwu(*(vusp)addr);
  480. break;
  481. case 4:
  482. *(vuip)addr = value;
  483. mb();
  484. *(vuip)addr;
  485. break;
  486. default:
  487. return PCIBIOS_FUNC_NOT_SUPPORTED;
  488. }
  489. return PCIBIOS_SUCCESSFUL;
  490. }
  491. struct pci_ops marvel_pci_ops =
  492. {
  493. .read = marvel_read_config,
  494. .write = marvel_write_config,
  495. };
  496. /*
  497. * Other PCI helper functions.
  498. */
  499. void
  500. marvel_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
  501. {
  502. io7_ioport_csrs *csrs = ((struct io7_port *)hose->sysdata)->csrs;
  503. wmb();
  504. csrs->POx_SG_TBIA.csr = 0;
  505. mb();
  506. csrs->POx_SG_TBIA.csr;
  507. }
  508. /*
  509. * RTC Support
  510. */
  511. struct marvel_rtc_access_info {
  512. unsigned long function;
  513. unsigned long index;
  514. unsigned long data;
  515. };
  516. static void
  517. __marvel_access_rtc(void *info)
  518. {
  519. struct marvel_rtc_access_info *rtc_access = info;
  520. register unsigned long __r0 __asm__("$0");
  521. register unsigned long __r16 __asm__("$16") = rtc_access->function;
  522. register unsigned long __r17 __asm__("$17") = rtc_access->index;
  523. register unsigned long __r18 __asm__("$18") = rtc_access->data;
  524. __asm__ __volatile__(
  525. "call_pal %4 # cserve rtc"
  526. : "=r"(__r16), "=r"(__r17), "=r"(__r18), "=r"(__r0)
  527. : "i"(PAL_cserve), "0"(__r16), "1"(__r17), "2"(__r18)
  528. : "$1", "$22", "$23", "$24", "$25");
  529. rtc_access->data = __r0;
  530. }
  531. static u8
  532. __marvel_rtc_io(u8 b, unsigned long addr, int write)
  533. {
  534. static u8 index = 0;
  535. struct marvel_rtc_access_info rtc_access;
  536. u8 ret = 0;
  537. switch(addr) {
  538. case 0x70: /* RTC_PORT(0) */
  539. if (write) index = b;
  540. ret = index;
  541. break;
  542. case 0x71: /* RTC_PORT(1) */
  543. rtc_access.index = index;
  544. rtc_access.data = bcd2bin(b);
  545. rtc_access.function = 0x48 + !write; /* GET/PUT_TOY */
  546. __marvel_access_rtc(&rtc_access);
  547. ret = bin2bcd(rtc_access.data);
  548. break;
  549. default:
  550. printk(KERN_WARNING "Illegal RTC port %lx\n", addr);
  551. break;
  552. }
  553. return ret;
  554. }
  555. /*
  556. * IO map support.
  557. */
  558. void __iomem *
  559. marvel_ioremap(unsigned long addr, unsigned long size)
  560. {
  561. struct pci_controller *hose;
  562. unsigned long baddr, last;
  563. struct vm_struct *area;
  564. unsigned long vaddr;
  565. unsigned long *ptes;
  566. unsigned long pfn;
  567. /*
  568. * Adjust the address.
  569. */
  570. FIXUP_MEMADDR_VGA(addr);
  571. /*
  572. * Find the hose.
  573. */
  574. for (hose = hose_head; hose; hose = hose->next) {
  575. if ((addr >> 32) == (hose->mem_space->start >> 32))
  576. break;
  577. }
  578. if (!hose)
  579. return NULL;
  580. /*
  581. * We have the hose - calculate the bus limits.
  582. */
  583. baddr = addr - hose->mem_space->start;
  584. last = baddr + size - 1;
  585. /*
  586. * Is it direct-mapped?
  587. */
  588. if ((baddr >= __direct_map_base) &&
  589. ((baddr + size - 1) < __direct_map_base + __direct_map_size)) {
  590. addr = IDENT_ADDR | (baddr - __direct_map_base);
  591. return (void __iomem *) addr;
  592. }
  593. /*
  594. * Check the scatter-gather arena.
  595. */
  596. if (hose->sg_pci &&
  597. baddr >= (unsigned long)hose->sg_pci->dma_base &&
  598. last < (unsigned long)hose->sg_pci->dma_base + hose->sg_pci->size) {
  599. /*
  600. * Adjust the limits (mappings must be page aligned)
  601. */
  602. baddr -= hose->sg_pci->dma_base;
  603. last -= hose->sg_pci->dma_base;
  604. baddr &= PAGE_MASK;
  605. size = PAGE_ALIGN(last) - baddr;
  606. /*
  607. * Map it.
  608. */
  609. area = get_vm_area(size, VM_IOREMAP);
  610. if (!area)
  611. return NULL;
  612. ptes = hose->sg_pci->ptes;
  613. for (vaddr = (unsigned long)area->addr;
  614. baddr <= last;
  615. baddr += PAGE_SIZE, vaddr += PAGE_SIZE) {
  616. pfn = ptes[baddr >> PAGE_SHIFT];
  617. if (!(pfn & 1)) {
  618. printk("ioremap failed... pte not valid...\n");
  619. vfree(area->addr);
  620. return NULL;
  621. }
  622. pfn >>= 1; /* make it a true pfn */
  623. if (__alpha_remap_area_pages(vaddr,
  624. pfn << PAGE_SHIFT,
  625. PAGE_SIZE, 0)) {
  626. printk("FAILED to map...\n");
  627. vfree(area->addr);
  628. return NULL;
  629. }
  630. }
  631. flush_tlb_all();
  632. vaddr = (unsigned long)area->addr + (addr & ~PAGE_MASK);
  633. return (void __iomem *) vaddr;
  634. }
  635. /* Assume it was already a reasonable address */
  636. vaddr = baddr + hose->mem_space->start;
  637. return (void __iomem *) vaddr;
  638. }
  639. void
  640. marvel_iounmap(volatile void __iomem *xaddr)
  641. {
  642. unsigned long addr = (unsigned long) xaddr;
  643. if (addr >= VMALLOC_START)
  644. vfree((void *)(PAGE_MASK & addr));
  645. }
  646. int
  647. marvel_is_mmio(const volatile void __iomem *xaddr)
  648. {
  649. unsigned long addr = (unsigned long) xaddr;
  650. if (addr >= VMALLOC_START)
  651. return 1;
  652. else
  653. return (addr & 0xFF000000UL) == 0;
  654. }
  655. #define __marvel_is_port_kbd(a) (((a) == 0x60) || ((a) == 0x64))
  656. #define __marvel_is_port_rtc(a) (((a) == 0x70) || ((a) == 0x71))
  657. void __iomem *marvel_ioportmap (unsigned long addr)
  658. {
  659. FIXUP_IOADDR_VGA(addr);
  660. return (void __iomem *)addr;
  661. }
  662. unsigned int
  663. marvel_ioread8(void __iomem *xaddr)
  664. {
  665. unsigned long addr = (unsigned long) xaddr;
  666. if (__marvel_is_port_kbd(addr))
  667. return 0;
  668. else if (__marvel_is_port_rtc(addr))
  669. return __marvel_rtc_io(0, addr, 0);
  670. else if (marvel_is_ioaddr(addr))
  671. return __kernel_ldbu(*(vucp)addr);
  672. else
  673. /* this should catch other legacy addresses
  674. that would normally fail on MARVEL,
  675. because there really is nothing there...
  676. */
  677. return ~0;
  678. }
  679. void
  680. marvel_iowrite8(u8 b, void __iomem *xaddr)
  681. {
  682. unsigned long addr = (unsigned long) xaddr;
  683. if (__marvel_is_port_kbd(addr))
  684. return;
  685. else if (__marvel_is_port_rtc(addr))
  686. __marvel_rtc_io(b, addr, 1);
  687. else if (marvel_is_ioaddr(addr))
  688. __kernel_stb(b, *(vucp)addr);
  689. }
  690. #ifndef CONFIG_ALPHA_GENERIC
  691. EXPORT_SYMBOL(marvel_ioremap);
  692. EXPORT_SYMBOL(marvel_iounmap);
  693. EXPORT_SYMBOL(marvel_is_mmio);
  694. EXPORT_SYMBOL(marvel_ioportmap);
  695. EXPORT_SYMBOL(marvel_ioread8);
  696. EXPORT_SYMBOL(marvel_iowrite8);
  697. #endif
  698. /*
  699. * NUMA Support
  700. */
  701. /**********
  702. * FIXME - for now each cpu is a node by itself
  703. * -- no real support for striped mode
  704. **********
  705. */
  706. int
  707. marvel_pa_to_nid(unsigned long pa)
  708. {
  709. int cpuid;
  710. if ((pa >> 43) & 1) /* I/O */
  711. cpuid = (~(pa >> 35) & 0xff);
  712. else /* mem */
  713. cpuid = ((pa >> 34) & 0x3) | ((pa >> (37 - 2)) & (0x1f << 2));
  714. return marvel_cpuid_to_nid(cpuid);
  715. }
  716. int
  717. marvel_cpuid_to_nid(int cpuid)
  718. {
  719. return cpuid;
  720. }
  721. unsigned long
  722. marvel_node_mem_start(int nid)
  723. {
  724. unsigned long pa;
  725. pa = (nid & 0x3) | ((nid & (0x1f << 2)) << 1);
  726. pa <<= 34;
  727. return pa;
  728. }
  729. unsigned long
  730. marvel_node_mem_size(int nid)
  731. {
  732. return 16UL * 1024 * 1024 * 1024; /* 16GB */
  733. }
  734. /*
  735. * AGP GART Support.
  736. */
  737. #include <linux/agp_backend.h>
  738. #include <asm/agp_backend.h>
  739. #include <linux/slab.h>
  740. #include <linux/delay.h>
  741. struct marvel_agp_aperture {
  742. struct pci_iommu_arena *arena;
  743. long pg_start;
  744. long pg_count;
  745. };
  746. static int
  747. marvel_agp_setup(alpha_agp_info *agp)
  748. {
  749. struct marvel_agp_aperture *aper;
  750. if (!alpha_agpgart_size)
  751. return -ENOMEM;
  752. aper = kmalloc(sizeof(*aper), GFP_KERNEL);
  753. if (aper == NULL) return -ENOMEM;
  754. aper->arena = agp->hose->sg_pci;
  755. aper->pg_count = alpha_agpgart_size / PAGE_SIZE;
  756. aper->pg_start = iommu_reserve(aper->arena, aper->pg_count,
  757. aper->pg_count - 1);
  758. if (aper->pg_start < 0) {
  759. printk(KERN_ERR "Failed to reserve AGP memory\n");
  760. kfree(aper);
  761. return -ENOMEM;
  762. }
  763. agp->aperture.bus_base =
  764. aper->arena->dma_base + aper->pg_start * PAGE_SIZE;
  765. agp->aperture.size = aper->pg_count * PAGE_SIZE;
  766. agp->aperture.sysdata = aper;
  767. return 0;
  768. }
  769. static void
  770. marvel_agp_cleanup(alpha_agp_info *agp)
  771. {
  772. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  773. int status;
  774. status = iommu_release(aper->arena, aper->pg_start, aper->pg_count);
  775. if (status == -EBUSY) {
  776. printk(KERN_WARNING
  777. "Attempted to release bound AGP memory - unbinding\n");
  778. iommu_unbind(aper->arena, aper->pg_start, aper->pg_count);
  779. status = iommu_release(aper->arena, aper->pg_start,
  780. aper->pg_count);
  781. }
  782. if (status < 0)
  783. printk(KERN_ERR "Failed to release AGP memory\n");
  784. kfree(aper);
  785. kfree(agp);
  786. }
  787. static int
  788. marvel_agp_configure(alpha_agp_info *agp)
  789. {
  790. io7_ioport_csrs *csrs = ((struct io7_port *)agp->hose->sysdata)->csrs;
  791. struct io7 *io7 = ((struct io7_port *)agp->hose->sysdata)->io7;
  792. unsigned int new_rate = 0;
  793. unsigned long agp_pll;
  794. /*
  795. * Check the requested mode against the PLL setting.
  796. * The agpgart_be code has not programmed the card yet,
  797. * so we can still tweak mode here.
  798. */
  799. agp_pll = io7->csrs->POx_RST[IO7_AGP_PORT].csr;
  800. switch(IO7_PLL_RNGB(agp_pll)) {
  801. case 0x4: /* 2x only */
  802. /*
  803. * The PLL is only programmed for 2x, so adjust the
  804. * rate to 2x, if necessary.
  805. */
  806. if (agp->mode.bits.rate != 2)
  807. new_rate = 2;
  808. break;
  809. case 0x6: /* 1x / 4x */
  810. /*
  811. * The PLL is programmed for 1x or 4x. Don't go faster
  812. * than requested, so if the requested rate is 2x, use 1x.
  813. */
  814. if (agp->mode.bits.rate == 2)
  815. new_rate = 1;
  816. break;
  817. default: /* ??????? */
  818. /*
  819. * Don't know what this PLL setting is, take the requested
  820. * rate, but warn the user.
  821. */
  822. printk("%s: unknown PLL setting RNGB=%lx (PLL6_CTL=%016lx)\n",
  823. __func__, IO7_PLL_RNGB(agp_pll), agp_pll);
  824. break;
  825. }
  826. /*
  827. * Set the new rate, if necessary.
  828. */
  829. if (new_rate) {
  830. printk("Requested AGP Rate %dX not compatible "
  831. "with PLL setting - using %dX\n",
  832. agp->mode.bits.rate,
  833. new_rate);
  834. agp->mode.bits.rate = new_rate;
  835. }
  836. printk("Enabling AGP on hose %d: %dX%s RQ %d\n",
  837. agp->hose->index, agp->mode.bits.rate,
  838. agp->mode.bits.sba ? " - SBA" : "", agp->mode.bits.rq);
  839. csrs->AGP_CMD.csr = agp->mode.lw;
  840. return 0;
  841. }
  842. static int
  843. marvel_agp_bind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  844. {
  845. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  846. return iommu_bind(aper->arena, aper->pg_start + pg_start,
  847. mem->page_count, mem->pages);
  848. }
  849. static int
  850. marvel_agp_unbind_memory(alpha_agp_info *agp, off_t pg_start, struct agp_memory *mem)
  851. {
  852. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  853. return iommu_unbind(aper->arena, aper->pg_start + pg_start,
  854. mem->page_count);
  855. }
  856. static unsigned long
  857. marvel_agp_translate(alpha_agp_info *agp, dma_addr_t addr)
  858. {
  859. struct marvel_agp_aperture *aper = agp->aperture.sysdata;
  860. unsigned long baddr = addr - aper->arena->dma_base;
  861. unsigned long pte;
  862. if (addr < agp->aperture.bus_base ||
  863. addr >= agp->aperture.bus_base + agp->aperture.size) {
  864. printk("%s: addr out of range\n", __func__);
  865. return -EINVAL;
  866. }
  867. pte = aper->arena->ptes[baddr >> PAGE_SHIFT];
  868. if (!(pte & 1)) {
  869. printk("%s: pte not valid\n", __func__);
  870. return -EINVAL;
  871. }
  872. return (pte >> 1) << PAGE_SHIFT;
  873. }
  874. struct alpha_agp_ops marvel_agp_ops =
  875. {
  876. .setup = marvel_agp_setup,
  877. .cleanup = marvel_agp_cleanup,
  878. .configure = marvel_agp_configure,
  879. .bind = marvel_agp_bind_memory,
  880. .unbind = marvel_agp_unbind_memory,
  881. .translate = marvel_agp_translate
  882. };
  883. alpha_agp_info *
  884. marvel_agp_info(void)
  885. {
  886. struct pci_controller *hose;
  887. io7_ioport_csrs *csrs;
  888. alpha_agp_info *agp;
  889. struct io7 *io7;
  890. /*
  891. * Find the first IO7 with an AGP card.
  892. *
  893. * FIXME -- there should be a better way (we want to be able to
  894. * specify and what if the agp card is not video???)
  895. */
  896. hose = NULL;
  897. for (io7 = NULL; (io7 = marvel_next_io7(io7)) != NULL; ) {
  898. struct pci_controller *h;
  899. vuip addr;
  900. if (!io7->ports[IO7_AGP_PORT].enabled)
  901. continue;
  902. h = io7->ports[IO7_AGP_PORT].hose;
  903. addr = (vuip)build_conf_addr(h, 0, PCI_DEVFN(5, 0), 0);
  904. if (*addr != 0xffffffffu) {
  905. hose = h;
  906. break;
  907. }
  908. }
  909. if (!hose || !hose->sg_pci)
  910. return NULL;
  911. printk("MARVEL - using hose %d as AGP\n", hose->index);
  912. /*
  913. * Get the csrs from the hose.
  914. */
  915. csrs = ((struct io7_port *)hose->sysdata)->csrs;
  916. /*
  917. * Allocate the info structure.
  918. */
  919. agp = kmalloc(sizeof(*agp), GFP_KERNEL);
  920. if (!agp)
  921. return NULL;
  922. /*
  923. * Fill it in.
  924. */
  925. agp->hose = hose;
  926. agp->private = NULL;
  927. agp->ops = &marvel_agp_ops;
  928. /*
  929. * Aperture - not configured until ops.setup().
  930. */
  931. agp->aperture.bus_base = 0;
  932. agp->aperture.size = 0;
  933. agp->aperture.sysdata = NULL;
  934. /*
  935. * Capabilities.
  936. *
  937. * NOTE: IO7 reports through AGP_STAT that it can support a read queue
  938. * depth of 17 (rq = 0x10). It actually only supports a depth of
  939. * 16 (rq = 0xf).
  940. */
  941. agp->capability.lw = csrs->AGP_STAT.csr;
  942. agp->capability.bits.rq = 0xf;
  943. /*
  944. * Mode.
  945. */
  946. agp->mode.lw = csrs->AGP_CMD.csr;
  947. return agp;
  948. }