core_marvel.c 25 KB

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